当前位置: 代码迷 >> Java Web开发 >> Servlet跳转有关问题
  详细解决方案

Servlet跳转有关问题

热度:51   发布时间:2016-04-17 15:44:23.0
Servlet跳转问题
在下写了两个Servlet,想第一个跳到第二个,但是运行后抛出这样的错误:
type:   Exception   report
message:
description:The   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.
exception:
java.lang.NullPointerException
  ex3_8.UserServlet2.doGet(UserServlet2.java:34)
  ex3_8.UserServlet2.doPost(UserServlet2.java:88)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  ex3_8.UserServlet1.doGet(UserServlet1.java:27)
  ex3_8.UserServlet1.doPost(UserServlet1.java:34)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

源代码:
customer.html
<html>
  <head> <title> 用户详细信息 </title> </head>
  <body>
    <form   method=post   action= "/ServletModule/userservlet1 ">
      <h1   align=center> 欢迎来到   Shop   Stop </h1>
      请您输入用户ID以显示您的详细信息:
      <br>
      <input   type=text   name=txtuserid>
      <br/>
      <input   type=submit   value= "提交 ">
    </form>
  </body>
</html>

UserServlet1:
package   ex3_8;
import   javax.servlet.*;
import   javax.servlet.http.*;
import   java.io.*;
import   java.util.*;

public   class   UserServlet1   extends   HttpServlet   {
        private   static   final   String   CONTENT_TYPE   =   "text/html;   charset=GBK ";

        //Initialize   global   variables
        public   void   init()   throws   ServletException   {
        }

        //Process   the   HTTP   Get   request
        public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)   throws
                        ServletException,   IOException   {
                String   uid=request.getParameter( "txtuserid ");
                response.setContentType(CONTENT_TYPE);
                PrintWriter   out   =   response.getWriter();
                ServletContext   context=getServletContext();
                context.setAttribute( "UserId ",uid);
                RequestDispatcher   dispatcher=request.getRequestDispatcher( "/userservlet2 ");
                if(dispatcher==null){
                                response.sendError(response.SC_NO_CONTENT);
                }
                dispatcher.forward(request,response);
  相关解决方案