先把出现的异常情况贴出来吧:
type Exception report
messagejava.lang.NullPointerException
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:177) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:91)
root cause
java.lang.NullPointerException org.apache.jsp.shiyan4.DataBaseOperation_jsp.<init>(DataBaseOperation_jsp.java:31) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) java.lang.Class.newInstance(Unknown Source) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:91)
noteThe full stack trace of the root cause is available in the Apache Tomcat/8.0.0-RC1 logs.
为什么会出现这种错误呢?一遍又一遍的检查自己的代码,发现需要一个javax.servlet.http.HttpServletRequest request;的声明,request的请求不能获取值。加上之后,得到这上面这样的异常:NullPointerException.
在几小时的挣扎中,想放弃了。。。但是,发现一个很隐秘的问题,这个潜伏在代码中的隐患,这是不容易查看到的。那就是将request获取值设置成全局变量。先看代码:
<body> <%! String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ; String DBURL = "jdbc:odbc:user" ; Connection conn = null ; //Statement stmt = null ; PreparedStatement stmt; String sql = null ; //ResultSet rs = null ; javax.servlet.http.HttpServletRequest request; //request.setCharacterEncoding("gb2312"); String username=request.getParameter("username"); //byte b[]=username.getBytes("ISO-8859-1"); //username=new String(b); String password=request.getParameter("password"); String address=request.getParameter("address"); String nation=request.getParameter("nation"); String sex=request.getParameter("sex"); //byte bb[]=sex.getBytes("ISO-8859-1"); //sex=new String(bb); String photo=request.getParameter("photo"); String temp=request.getParameter("age"); %>
随后,修改后尝试,发现问题解决了。
于是得出结论:在JSP中,获取表单提交的值时,不能将获取语句放在全局变量里面,即不能放在<%! %>之间。(注意感叹号!),而是放在<% %>中。
为了同大家交流,遂贴上所有源码,望各位指点:
//regedit.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>用户注册</title> <script language="JavaScript"> function change() { var photo = document.getElementById("photo"); var photoImg = document.getElementById("photoImg"); photoImg.src = photo.value; } </script> </head> <link rel="stylesheet" href="style.css" type="text/css" /> <body> <div class="div1"> <div class="div2"><h2>输入用户信息</h2></div> <form action="DataBaseOperation.jsp" method="post" name="form"> <table align="center"> <tr > <td class="div3">用户名</td> <td><input type="text" name="username" /></td> </tr> <tr > <td class="div3">密码</td> <td><input type="password" name="password" /></td> </tr> <tr> <td class="div3">年龄</td> <td><input type="text" name="age" /></td> </tr> <tr> <td class="div3">出生地</td> <td><input type="text" name="address" /></td> </tr> <tr> <td class="div3">民族</td> <td><input type="text" name="nation" /></td> </tr> <tr> <td class="div3">性别</td> <td><input type="radio" name="sex" value="男" />男 <input type="radio" name="sex" value="女" />女</td> </tr> <tr> <td class="div3">头像</td> <td align="left"> <select name="photo" id="photo" onchange="change();"> <option value="images/01.gif" selected="selected">头像一</option> <option value="images/02.gif">头像二</option> </select> <img id="photoImg" src="images/01.gif"> </td> </tr> <tr> <td></td> <td align="left"><input type="submit" name="submit" value="注册" /> <input type="reset" name="reset" value="重置" /> </td> </tr> </table> </form> </div> </body> </html>
//style.css
@charset "utf-8"; /* CSS Document */ body { border:0px; margin-top:0px; margin-left:0px; background:url(images/bg4.jpg); } table { padding-top:5px; left:340px; padding:20px 0px 3px 20px; } .div1 { background:url(images/bg3.jpg); width:900px; height:760px; text-align:center; margin-top:0px; margin-left:225px; } .div2 { margin-top:0px; } .div1 table tr { /*line-height:30px;*/ height:30px;} .div3 { text-align:right; }
//DataBaseOperation.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>注册信息</title> </head> <body> <%! String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ; String DBURL = "jdbc:odbc:user1" ; Connection conn = null ; //Statement stmt = null ; PreparedStatement stmt; String sql = null ; //ResultSet rs = null ; %> <% //javax.servlet.http.HttpServletRequest request; request.setCharacterEncoding("gb2312"); String username=request.getParameter("username"); //byte b[]=username.getBytes("ISO-8859-1"); //username=new String(b); String password=request.getParameter("password"); String address=request.getParameter("address"); String nation=request.getParameter("nation"); String sex=request.getParameter("sex"); //byte bb[]=sex.getBytes("ISO-8859-1"); //sex=new String(bb); //String photo=request.getParameter("photo"); String temp=request.getParameter("age"); if(temp==null) { temp=""; } Integer age=Integer.parseInt(temp); try { Class.forName(DBDRIVER) ; } catch(Exception e) { } try { conn = DriverManager.getConnection(DBURL) ; } catch(Exception e) { } try { sql="Insert into user(name,password,age,address,nation,sex) values(?,?,?,?,?,?)"; stmt=conn.prepareStatement(sql); stmt.setString(1,username); stmt.setString(2,password); stmt.setInt(3,age); stmt.setString(4,address); stmt.setString(5,nation); stmt.setString(6,sex); //stmt.setString(7,photo); stmt.executeUpdate(); //stmt = conn.createStatement() ; // sql="Insert into user(name,password,age,address,nation,sex,images) values('"+username+"','"+password+"','"+sex+"')"; //stmt.executeUpdate(sql); } catch(Exception e) { } try { stmt.close() ; conn.close() ; out.println("注册成功!"); %> 请记住您的注册信息:<br /> 用户名:<%=username%><br /> 密码:<%=password%> <br /> <% } catch(Exception e) { out.println("数据库关闭失败!!!") ; } %> </body> </html>
//数据库
(这个我上传到百度云后,给链接。)
功能不太完善,当时我创建的数据库中头像的类型是文本(Varchar)的,表单获取的头像的值是头像的src,但是不知道为什么,存放不进去。于是,上面SQL语句中没有插入头像。希望大侠指点一下,在mysql数据库中,图像的存放时什么类型,或者怎么处理。
,