2016-05-28 7 views
0

JSPを学習しようとしていますが、JSPの仕組みを調べるための簡単なプロジェクトを実装しましたが、次のエラーが発生します。サーバー上でアプリケーションを実行します。私はリンクにユーザー名を入力する必要が何かを確認するには:java.lang.NullPointerException org.user.SimpleServlet.doGet(SimpleServlet.java:31)

http://localhost:8082/SimpleServletProject_6_passing_more_parameters/SimpleServletPath?userName=alex

が、userNameに

/SimpleServletProject_6_passing_more_parameters/SimpleServletPath

せずにリンクを入力するとき、私は以下のエラーを取得しています。

SimpleServletのクラス:

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")); 

    } 
} 

エラー:

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) 

web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 
    <display-name>SimpleServletProject_6_passing_more_parameters</display-name> 

</web-app> 
+0

を?? – emotionlessbananas

+0

とそのリンクはローカルマシンでのみ有効です – emotionlessbananas

+0

@holidayCoder:私はそれを追加しました。 –

答えて

1

あなたがチェックインしていますgはユーザ名が空でないことを確認する前に確認してください。nullです。実際にnullの場合、isEmpty()への呼び出しは失敗し、NullPointerExceptionとなります。ただ、周りのチェックを切り替える:

if (userName != null && !userName.isEmpty()) { 
    // code 

それともは、Apache Commonsのラングを使用することができれば、あなたはよりエレガントな成果のためにStringUtils#isEmptyを使用することができますuはスタックトレースを印刷することができます

if (!StringUtils.isEmpty(userName)) { 
    // code 
+0

が表示されていますが、「500」エラーが表示されています – emotionlessbananas

+0

@holidayCoder '500' - **内部サーバーエラー**これは、アプリケーションサーバーによって発生する典型的なエラーです。 –

関連する問題