初学jsp就遇到了这样的问题? 求大神指教

菜鸟刘 发布于 2016/02/24 21:48
阅读 448
收藏 0

【领华为电脑包】容器化时代到来!跳转机分配问题终于“有救”了!>>>

jsp文件内容:

<%@page import="java.util.Vector"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
 <%@ page import="java.util.Vector" %>
 <%@ page import="java.util.Enumeration" %>
 <%@ include file="session.html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>session server</title>
</head>
<body>
<%--定义一个名为movies的vector,并将其初始化为null --%>
<%!Vector movies=null; %>
<%      
if((request.getParameter("submit")).equals("注销")){
session.invalidate();//结束本session
out.println("再见!\n");
}
else{
//判断是否是新的session,是则创建新的电影列表
if(session.isNew()){
movies=new Vector();
//赋值给session
session.setAttribute("movies", movies);
}
else{
//否则取出已有列表
movies=(Vector)session.getAttribute("movies");
}
//在列表中添加电影
if(request.getParameter("submit")==null||(request.getParameter("submit")).equals("添加")){
movies.addElement((String)request.getParameter("item"));
}
//在列表中删除电影
if(request.getParameter("submit")==null||request.getParameter("submit").equals("删除")){
movies.addElement((String)request.getParameter("item"));
}
//给session赋值,所附的值是修改过的值
session.getAttribute("movies");
out.println("<font size=3 color=blue><br>你选择了下面这些电影:</font>");
out.println("<hr><font size=3 color='#cc0000'>");
if(movies!=null){
Enumeration<String> enumeration=movies.elements();
out.println("<o1>");
while(enumeration.hasMoreElements()){
out.println("<1i>"+(String)enumeration.nextElement()+"<1i>");
}
out.println("</o1");
}
}
%><hr>

</body>

</html>

html文件内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session client</title>
</head>
<body>
<form type=post action="session.jsp">
<font size=5 color=blue>
添加或删除你喜欢的电影<br>
请选择:<font>
<select name=item>
<option>功夫熊猫
<option>机器人
<option>斯巴达勇士
<option>音乐之声
<option>阿甘正传
<option>史前以往年
</select><br>
<input type=submit name="submit" value="添加">
<input type=submit name="submit" value="删除">
<input type=submit name="submit" value="注销">
</font>
</font>
</form>
</body>
</html>

前提是另一个html文件没有问题,上面是代码,运行时的错误如下:



二月 24, 2016 9:38:28 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/web_excise] threw exception [An exception occurred processing JSP page /session.jsp at line 17


14: <%--瀹氫箟涓?釜鍚嶄负movies鐨剉ector锛屽苟灏嗗叾鍒濆鍖栦负null --%>
15: <%!Vector movies=null; %>
16: <%      
17: if((request.getParameter("submit")).equals("娉ㄩ攢")){
18: session.invalidate();//缁撴潫鏈瑂ession
19: out.println("鍐嶈锛乗n");
20: }



二月 24, 2016 9:38:28 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/web_excise] threw exception [An exception occurred processing JSP page /session.jsp at line 17


14: <%--瀹氫箟涓?釜鍚嶄负movies鐨剉ector锛屽苟灏嗗叾鍒濆鍖栦负null --%>
15: <%!Vector movies=null; %>
16: <%      
17: if((request.getParameter("submit")).equals("娉ㄩ攢")){
18: session.invalidate();//缁撴潫鏈瑂ession
19: out.println("鍐嶈锛乗n");
20: }
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.session_jsp._jspService(session_jsp.java:165)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)




加载中
0
f
freezingsky
(request.getParameter("submit"))这一句不能保证一定有值。也就是说,有可能是Null.
f
foreverLast
回复 @菜鸟刘 : request.getParameter("submit") 你输出一下这个值
菜鸟刘
菜鸟刘
回复 @菜鸟刘 不行,一运行还是有错??? :
f
freezingsky
回复 @菜鸟刘 : if((request.getParameter("submit")).equals("娉ㄩ攢"))换成 if("娉ㄩ攢".equals(request.getParameter("submit")))
菜鸟刘
菜鸟刘
你说的这是哪,我没明白,请说的详细点
0
VitoHuang
VitoHuang
传过来的参数submit不一定是有值的!这种比较方式容易异常!应该改成if("注销".equals(request.getParameter("submit"))) 学到EL表达式后 这些会变得更加简便
菜鸟刘
菜鸟刘
不行,一运行还是有错???
0
菜鸟刘
菜鸟刘

不行,一运行还是有错???

菜鸟刘
菜鸟刘
你们看看,把代码复制过去用你们的试试
0
爱生活_爱JAVA
爱生活_爱JAVA
15 行 应该是  == 二个,而不是 1 个 = 号
菜鸟刘
菜鸟刘
回复 @foreverLast : 那你说的是哪个
菜鸟刘
菜鸟刘
不对,这样不行
f
foreverLast
那个是定义吧
0
Happy猪猪
Happy猪猪

看下 request.getParameter("submit") 的值 是不是null

菜鸟刘
菜鸟刘
不会啊
返回顶部
顶部