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);
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
UserServlet2:
package ex3_8;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class UserServlet2 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 UserPassword=null;
String FirstName=null;
String LastName=null;
String Age=null;
String Gender=null;
ServletContext context=getServletContext();
Object obj=context.getAttribute( "userid ");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <title> CusterDetails </title> </head> ");
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
Connection con=DriverManager.getConnection( "jdbc:odbc:petclinicapps ", "administrator ", "GSLwenbaihua ");
Statement s=con.createStatement();
ResultSet rs=s.executeQuery( "Select * from CustomerRegistration where UserId ' "+obj.toString().trim()+ " ' ");
boolean flag=rs.next();
if (flag==true) {
UserPassword=rs.getString(2);
FirstName=rs.getString(3);
LastName=rs.getString(4);
Gender=rs.getString(5);
Age=rs.getString(6);
}else{
out.println( " <font color=red size=7> 登录失败 </font> ");
}
}
catch(java.sql.SQLException sqle){
out.println( " <font color=red size=7> "+sqle.toString()+ " </font> ");
}
catch(ClassNotFoundException e){
out.println( "类未找到异常 "+e);
}
out.println( " <body> ");
out.println( " <H1 align=center> 欢迎来到Shop Stop </H1> ");
out.println( " <table width=80% border=0 align=center> ");
out.println( " <tr align=center valign=top> ");
out.println( " <td colspan=2> <h1> <em> <strong> 客户详细信息 </strong> </em> </h1> </td> ");
out.println( " </tr> ");
out.println( " <tr> ");
out.println( " <td width=25%> <span> <strong> 用户 ID: </strong> </span> </td> ");
out.println( " <td width=75%> <span> "+obj.toString()+ " </span> </td> ");
out.println( " </tr> ");
out.println( " <tr> ");
out.println( " <td> <span> <strong> 口令: </strong> </span> </td> ");
out.println( " <td> <span> "+UserPassword+ " </span> </td ");
out.println( " <tr> ");
out.println( " </tr> ");
out.println( " <td> <span> <strong> 全名: </strong> </span> </td> ");
out.println( " <td> <span> "+FirstName+ " "+LastName+ " </span> </td> ");
out.println( " </tr> ");
out.println( " <tr> ");
out.println( " <td> <span> <strong> 性别: </strong> </span> </td> ");
out.println( " <td> "+Gender+ " </td> ");
out.println( " </tr> ");
out.println( " <tr> ");
out.println( " <td> <span> <strong> 年龄: </strong> </span> </td> ");
out.println( " <td> "+Age);
out.println( " <span> 岁 </span> </td> ");
out.println( " </tr> ");
out.println( " </table> ");
out.println( " </html> ");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
[解决办法]
空引用
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html