HttpSession出了什么问题



你好,我有上面的代码

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class CarSessionServlet2 extends HttpServlet {
    private String Brands[] = {
        "Audi", "Ford", "Toyota"
    };
    private String Cars[] = {
        "A3", "Fiesta", "Yaris"
    };
    private String screen = "name";
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // reaction

        if (screen.equals("name")) { //elegxos apo poia selida kalw tin doPost. Edw apo tin BrandsSelection.html
            String cookieName = request.getParameter("name"); // choice made
            // will be sent
            // back to
            // client
            String cookieBrand = request.getParameter("brands");
            PrintWriter output;
            HttpSession session = request.getSession(true);
            session.setAttribute("name", cookieName);


            session.setAttribute(cookieBrand, getCars(cookieBrand));
            response.setContentType("text/html");
            output = response.getWriter();
            // send HTML page to client
            output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
            output.println("Cookies");
            output.println("</TITLE></HEAD><BODY>");
            output.println("<P>Welcome Ms./Mr.: ");
            // output.println( "<P>" );
            output.println(cookieName);
            output.println(" <BR> you have chosen ");
            output.println(cookieBrand);
            output.println("<BR><a href='SecondPage.html'>" + "Click here to continue..." + "</a>");
            output.println("</BODY></HTML>");
            output.close(); // close stream
            screen = "color";

        }
        if (screen.equals("color")) { //elegxos apo poia selida kalw tin doPost. Edw apo tin SecondPage.html
            PrintWriter output;
            String color = request.getParameter("color");
            HttpSession session = request.getSession(true);
            session.setAttribute("color", color);
            response.setContentType("text/html");
            output = response.getWriter();
            // send HTML page to client
            output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
            output.println("Cookies");
            output.println("</TITLE></HEAD><BODY>");
            output.println("<FORM ACTION="http://localhost:8080/Askisi2Session/CarSessionServlet2" METHOD="GET">");
            output.println("<STRONG>To see your selections press the button:<br> </STRONG>");
            output.println("<INPUT TYPE="submit" VALUE="Selections">");
            output.println("</FORM>");
            output.println("</BODY></HTML>");
            output.close(); // close stream
            screen = "name";
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // reaction
        // to
        // the
        // reception
        PrintWriter output; // of
        // GET
        response.setContentType("text/html");
        output = response.getWriter();
        output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
        output.println("Cookie with your selections has been read !");
        output.println("</TITLE></HEAD><BODY>");
        HttpSession session = request.getSession(false); // get client's session;
        output.println("<H3>Here is your saved session data:</H3>");
        Enumeration e;
        if (session != null) {
            e = session.getAttributeNames();
        } else {
            e = null;
        }
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            output.println(name + ": " + session.getAttribute(name) + "<BR>");
        }


        output.println("<a href='BrandsSelection.html'>Click here to Restart...</a>");
        output.println("</BODY></HTML>");
        output.close(); // close stream
    }
    private String getCars(String conString) {
        for (int i = 0; i < Brands.length; ++i)
        if (conString.equals(Brands[i])) {
            return Cars[i];
        }
        return ""; // no matching string found
    }
} // END OF CLASS CookieServlet

当我用tomcat 8.0.28运行它时,我得到了一个提到的java.lang.NullPointerException

java.lang.NullPointerException
CarSessionServlet2.getCars(CarSessionServlet2.java:155)
CarSessionServlet2.doPost(CarSessionServlet2.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

第43行是

session.setAttribute(cookieBrand,getCars(cookieBrand));

第155行是

if (conString.equals(Brands[i])) {

首先,我打开一个html页面,填充一个文本框,在单选按钮上做出选择,然后按下一个调用doPost方法的提交按钮。然后出现第二个html页面。我填充了另一个文本框,然后按下另一个提交按钮,我再次调用doPost方法(第二个if语句,screen=color)。然后。。。。。NullPointerException。它应该显示另一个html页面,其中包含一个调用doGet方法的按钮。我不知道为什么我采取的例外

Html第1页(品牌选择)

<HTML>
<HEAD>
   <TITLE>Cookie will be written in our disc</TITLE> 
</HEAD>
<BODY>
   <FORM ACTION="http://localhost:8080/Askisi2Session/CarSessionServlet2" METHOD="POST">      
      <STRONG>Enter Your Name:<br> </STRONG>
      <PRE>
      <INPUT TYPE="text" NAME="name"><br><br>
      <STRONG>Select the Brand of your Desire:<br> </STRONG>
      <PRE>
      <INPUT TYPE="radio" NAME="brands" VALUE="Audi">check here for Audi<BR>
      <INPUT TYPE="radio" NAME="brands" VALUE="Ford">check here for Ford<BR>
      <INPUT TYPE="radio" NAME="brands" VALUE="Toyota" CHECKED>check here for Toyota<BR>
      </PRE>
      <INPUT TYPE="submit" VALUE="Submit">
      <INPUT TYPE="reset"> </P>
   </FORM>
</BODY>
</HTML>

HTML第2页(SeondPage)

<HTML>
<HEAD> 
   <TITLE>Cookie taken into Account</TITLE> 
</HEAD>
<BODY>
   <FORM ACTION="http://localhost:8080/Askisi2Session/CarSessionServlet2" METHOD="POST">
      <STRONG>What is your favorite color?<br> </STRONG>
      <PRE>
      <INPUT TYPE="text" NAME="color"><br>
      </PRE>
      <INPUT TYPE="submit" VALUE="Submit">
   </FORM>
</BODY>
</HTML>

此方法getCars(...)返回"",因为条件if(conString.equals(Brands[i]))与任何字符串都不匹配:

    private String getCars(String conString) {
    for (int i = 0; i < Brands.length; ++i)
    if (conString.equals(Brands[i])) {
        return Cars[i];
    }
    return ""; 
    }

相关内容

  • 没有找到相关文章

最新更新