我正在尝试学习JSP,但我已经实现了一个简单的项目来检查JSP是如何工作的,但是当我在服务器上运行应用程序时,我收到以下错误。要查看内容,我必须在链接中输入用户名:
http://localhost:8082/SimpleServletProject_6_passing_more_parameters/SimpleServletPath?userName=alex
但输入不带userName的链接
/ SimpleServletProject_6_passing_more_parameters / SimpleServletPath
我收到以下错误。
SimpleServlet类:
std::complex
错误:
package org.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class SimpleServlet
*/
@WebServlet(description = "A simple servlet", urlPatterns = { "/SimpleServletPath" })
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String userName = request.getParameter("userName");
HttpSession session = request.getSession();
if (!userName.isEmpty() && userName != null) {
session.setAttribute("savedNameUser", userName);
}
writer.println("Request parameter has username as " + userName);
writer.println("Session parameter has username as "
+ (String) session.getAttribute("savedNameUser"));
}
}
的web.xml
Mai 28, 2016 9:14:10 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SimpleServletProject_6_passing_more_parameters' did not find a matching property.
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server version: Apache Tomcat/7.0.69
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server built: Apr 11 2016 07:57:09 UTC
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server number: 7.0.69.0
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Name: Windows 8.1
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Version: 6.3
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Architecture: amd64
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Java Home: C:\Program Files\Java\jre1.8.0_74
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Version: 1.8.0_74-b02
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Vendor: Oracle Corporation
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_BASE: G:\eclipse_workspace\Java_JSP\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 7.0
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.base=G:\eclipse_workspace\Java_JSP\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 7.0
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dwtp.deploy=G:\eclipse_workspace\Java_JSP\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 7.0\endorsed
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dfile.encoding=Cp1252
Mai 28, 2016 9:14:10 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMATION: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_74\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_74/bin/server;C:/Program Files/Java/jre1.8.0_74/bin;C:/Program Files/Java/jre1.8.0_74/lib/amd64;C:\xampp\ImageMagick-7.0.1-Q16;C:\Program Files\ImageMagick-7.0.1-Q16;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;;G:\eclipse\eclipse for dynamic web\eclipse;;.
Mai 28, 2016 9:14:10 AM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-bio-8082"]
Mai 28, 2016 9:14:10 AM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["ajp-bio-8009"]
Mai 28, 2016 9:14:10 AM org.apache.catalina.startup.Catalina load
INFORMATION: Initialization processed in 942 ms
Mai 28, 2016 9:14:10 AM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service Catalina
Mai 28, 2016 9:14:10 AM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/7.0.69
Mai 28, 2016 9:14:11 AM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["http-bio-8082"]
Mai 28, 2016 9:14:11 AM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["ajp-bio-8009"]
Mai 28, 2016 9:14:11 AM org.apache.catalina.startup.Catalina start
INFORMATION: Server startup in 1175 ms
Mai 28, 2016 9:14:12 AM org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet [org.user.SimpleServlet] in context with path [/SimpleServletProject_6_passing_more_parameters] threw exception
java.lang.NullPointerException
at org.user.SimpleServlet.doGet(SimpleServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
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:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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 :(得分:1)
您需要检查用户名是否为空,然后才能查看null
。如果它真的null
,则对isEmpty()
的调用将失败并显示NullPointerException
。只需切换支票:
if (userName != null && !userName.isEmpty()) {
// code
或者如果您可以使用Apache Commons Lang,您可以使用StringUtils#isEmpty
获得更优雅的结果:
if (!StringUtils.isEmpty(userName)) {
// code