我正在制作一个带有用户个人资料的Java网络应用程序
我有一个profileServlet,负责呈现两个页面:profile.jsp和editProfile.jsp
调用profileServlet的第一个源是页面顶部的菜单链接,如下所示:
<a href="profileServlet?param=profile">Profile</a>
第二个来源是profile.jsp中的“编辑”按钮:
<form name="editProfile" method="GET" action="profileServlet?param=editProf"><input type="submit" value="Edit"></form>
现在在profileServlet中,根据我想要重定向到profile.jsp或editProfile.jsp的param值。这是代码:
if(request.getParameter("param").equals("profile")) {
response.sendRedirect("profile.jsp");
} else if (request.getParameter("param").equals("editProf")) {
response.sendRedirect("editProfile.jsp");
}
但我一直得到java.lang.NullPointerException 你能帮忙吗?
Stacktrace:
24.04.2014 11:44:30 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [profilePage.profileServlet] in context with path [/LinkedIn] threw exception
java.lang.NullPointerException
at profilePage.profileServlet.doGet(profileServlet.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
您的请求参数param
可能为null,您可以像下面这样执行,也可以在使用变量之前进行适当的空检查
if ("profile".equals(request.getParameter("param")) {
response.sendRedirect("profile.jsp");
} else if ("editProf".equals(request.getParameter("param")) {
response.sendRedirect("editProfile.jsp");
}
此外,请确保将代码放在各自的doGet
/ doPost
处理程序中,或者为两者都设置一个处理程序。
示例:强>
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
handleGetAndPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
handleGetAndPost(request, response);
}
private void handleGetAndPost(HttpServletRequest request, HttpServletResponse response) {
if ("profile".equals(request.getParameter("param")) {
response.sendRedirect("profile.jsp");
} else if ("editProf".equals(request.getParameter("param")) {
response.sendRedirect("editProfile.jsp");
}
}
答案 1 :(得分:0)
您应该在提交表单时使用帖子请求,并且有两种不同的处理程序方法。 GET返回空表单,POST处理提交的表单。
答案 2 :(得分:-1)
最好添加空检查。看起来你无法获得param
if (request.getParameter("param") != null && request.getParameter("param").equals("profile")) {
response.sendRedirect("profile.jsp");
} else if (request.getParameter("param") != null && request.getParameter("param").equals("editProf")) {
response.sendRedirect("editProfile.jsp");
}