用jsp写好的registerForm.jsp注册页面跳转到register.jsp页面执行,出现以下错误
(另附上register.jsp代码)
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /register.jsp at line 3229:    String requsername=request.getParameter("username");//接收参数username
30:    String reqpassword=request.getParameter("userpassword");//接收参数password 
31:    %>
32:    <%
33:    String url="jdbc:sqlserver://localhost:1433;DatabaseName=news"; //url地址,news是数据库名称
34:    String users="rr";
35:    String password="r1";
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause java.lang.NullPointerException
org.apache.jsp.register_jsp._jspService(register_jsp.java:111)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'register.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
    <%
   request.setCharacterEncoding("gb2312");//设置接收编码格式
   String requsername=request.getParameter("username");//接收参数username
   String reqpassword=request.getParameter("userpassword");//接收参数password 
   %>
   <%
   String url="jdbc:sqlserver://localhost:1433;DatabaseName=news"; //url地址,news是数据库名称
   String users="rr";
   String password="r1";
   Connection conn=null;
   PreparedStatement pstmt=null;
  try{
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//加载jdbc驱动程序
   conn=java.sql.DriverManager.getConnection(url,users,password);//连接数据库
}
catch(ClassNotFoundException e)
{
out.println("找不到驱动类");//抛出异常,提示错误
}
catch(SQLException e){
out.println("连接数据库失败!");
}
try{
 String adduser="insert into user values(null,?,?)";//增加一条用户信息
 pstmt=conn.prepareStatement(adduser); //创建语句对象prepareStatement
 pstmt.setString(1,requsername);//设置参数,接收到的用户名
 pstmt.setString(2,reqpassword);//设置参数,接收到的密码
 pstmt.executeUpdate();//添加注册用户
 out.println("用户注册成功,请登录");
 
}
catch(SQLException e){
out.println("添加用户信息失败!");
}
//关闭数据库连接
try{if(pstmt!=null){
pstmt.close();  //关闭预处理语句对象
pstmt=null;
}
if(conn!=null){
conn.close();//关闭数据库连接
conn=null;
}
}catch(Exception e){
out.println("数据库关闭异常!");
}
%>
<jsp:forward page="login.jsp"></jsp:forward>
  </body>
</html>

解决方案 »

  1.   


    29: String requsername=request.getParameter("username");//接收参数username
    30: String reqpassword=request.getParameter("userpassword");//接收参数password  
    不需要.toString吗???
      

  2.   

    把接受到的转换成字符串,比如说  userName=new String(userName.getByte("ISO-8859-1"),"UTF-8")
      

  3.   

     pstmt.setString(1,requsername);//设置参数,接收到的用户名
     pstmt.setString(2,reqpassword);//设置参数,接收到的密码
    这个看看有没有问题,看看requsername跟reqpassword是否有值,如果没值这里会报错!
    如果是这个原因,你看看是不是你的不是用form提交过来的,你是否在jsp提交时忘记用form标签了。
      

  4.   

    可以做下判断
    if(request.getParameter("username")!=null)
     {
     String requsername=request.getParameter("username");//接收参数username
     }
    if(request.getParameter("userpassword")!=null)
      {
    String reqpassword=request.getParameter("userpassword");//接收参数password 
      } 
      

  5.   

    看到空指针异常,最好页面输出调一下,不过这种不用连接池的写法在实际中没人这么用,JSP十SqLserver也不好,Oracle可能更多与Java配合多