gpt4 book ai didi

java - HttpSession 有什么问题?

转载 作者:行者123 更新时间:2023-11-29 00:01:46 24 4
gpt4 key购买 nike

<分区>

你好,我有上面的代码

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。它应该出现另一个带有调用 doGet 方法的按钮的 html 页面。我不知道为什么我要异常(exception)

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 页(第二页)

<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>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com