servlet小问题。从页面读写东西到数据库,出的问题。在线等
问题描述:一个通过html from表单提交一些数据,并且写入数据库。现在问题如下:HTTP Status 500 -
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.play.RegistrationWithHttpSession.doGet(RegistrationWithHttpSession.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
网页html代码如下:
<html>
<head>
<title>
Registration using session
</title>
</head>
<body>
<form method="GET"
action="RegistrationWithHttpSession">
<p> Last name <font color="#FF000">*</font>
<input type="text" name="lastName">
First name <font color="#FF000">*</font>
<input type="text" name="firstName">
Mi <font color="#FF000">*</font>
<input type="text" name="mi" size="3"></p>
<p>Telephone <input type="text" name="telephone" size="20">
Email <input type="text" name="email" size="30">
<p>Street<input type="text" name="street" size="25">
<p>City<input type="text" name="city" size="28">
State <select size="1" name="state">
<option value="GA">Georgia-GA</option>
<option value="OK">Georgia-OK</option>
<option value="IN">Georgia-IN</option>
</select >
Zip <input type="text" name="zip" size="9"></p>
<p><input type="submit" name="Submit" value="Submit" >
<input type="reset" value="Reset"></p>
</from>
<p><font color="#FF00000">* required fields </font ></p>
</body>
</html>
java代码:package com.play;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RegistrationWithHttpSession extends HttpServlet{
private static final long serialVersionUID = 1L;
private PreparedStatement pstmt;
public void init()throws ServletException{
System.out.println("22222");
initializeJdbc();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException,IOException{
response.setContentType("html/text;charset=GBK");
PrintWriter out = response.getWriter();
String lastName=request.getParameter("lastName");
String firstName=request.getParameter("firstName");
String mi=request.getParameter("mi");
String telephone=request.getParameter("telephone");
String email=request.getParameter("email");
String street=request.getParameter("street");
String city=request.getParameter("city");
String state=request.getParameter("state");
String zip=request.getParameter("zip");
if(lastName.length()==0||firstName.length()==0){
out.println("last name and first name are required!");
return;
}
Student student=new Student(lastName,firstName,mi,telephone,email,
street,city,state,zip);
HttpSession httpsession=request.getSession();
System.out.println("33333333");
httpsession.setAttribute("student", student);
out.println("you entered the following data :");
out.println("<p> Last name :"+lastName);
out.println("<p> First name :"+ firstName);
out.println("<p> Mi:"+mi);
out.println("<p> Telephone :"+ telephone);
out.println("<p> Email :"+email);
out.println("<p> City :"+ city);
out.println("<p> State :"+ state);
out.println("<p> zip :"+ zip);
out.println("<p><form method=\"POST\" action="+"/my/RegistrationWithHttpSession>");
out.println("<p><input type=\"submit\" value=\"Confirm\" >");
out.println("</form>");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws
ServletException,IOException{
response.setContentType("html/text;charset=GBK");
PrintWriter out=response.getWriter();
HttpSession httpsession =request.getSession();
System.out.println("444444444");
Student student =(Student)(httpsession.getAttribute("student"));
try{
storeStudent(student);
out.println(student.getFirstName()+""+student.getLastName()+"is now registered in the database");
out.close();
}catch(Exception ex){
out.println("Error :"+ex.getMessage());
return;
}
}
private void storeStudent(Student student) throws SQLException{
pstmt.setString(1, student.getLastName());
pstmt.setString(2, student.getFirstName());
pstmt.setString(3, student.getMi());
pstmt.setString(4, student.getTelephone());
pstmt.setString(5, student.getEmail());
pstmt.setString(6, student.getStreet());
pstmt.setString(7, student.getCity());
pstmt.setString(8, student.getState());
pstmt.setString(9, student.getZip());
System.out.println("555555555");
}
private void initializeJdbc() {
Connection conn=null;
try{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mysql";
String user ="root";
String password="root";
Class.forName(driver);
conn =DriverManager.getConnection(url,user,password);
pstmt=conn.prepareStatement("insert into Address"+"(lastName,firstName,mi,telephone,email," +
"street,city,state,zip) values(?,?,?,?,?,?,?,?)");
System.out.println("566666");
}catch(Exception ee){
System.out.println("++++");
System.out.println(ee);
}finally{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.play;
public class Student {
private String lastName="";
private String firstName="";
private String mi="";
private String telephone="";
private String email="";
private String street="";
private String city="";
private String state="";
private String zip="";
public Student(String lastName,String firstName,String mi,String telephone,
String email,String street,String city,String state,String zip){
this.lastName=lastName;
this.firstName=firstName;
this.mi=mi;
this.telephone=telephone;
this.state=state;
this.street=street;
this.zip=zip;
System.out.println("1111111111");
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getMi() {
return mi;
}
public String getTelephone() {
return telephone;
}
public String getEmail() {
return email;
}
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
}
servlet web.xml 配置如下;
<servlet>
<servlet-name>RegistrationWithHttpSession</servlet-name>
<servlet-class>com.play.RegistrationWithHttpSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistrationWithHttpSession</servlet-name>
<url-pattern>/RegistrationWithHttpSession</url-pattern>
</servlet-mapping>