servlet小问题。从页面读写东西到数据库,出的问题。在线等

a380627112 2010-07-20 09:55:47
问题描述:一个通过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>

...全文
97 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
a380627112 2010-07-21
  • 打赏
  • 举报
回复
好,我把这
if("".equals(lastName)||"".equals(firstName)||lastName.length()==0||firstName.length()==0){
out.println("last name and first name are required!");
return;
}
代码注释掉,结果依然写不进数据库,一直出来下载页面。为什么?数据库那一段写的有问题??
luther_liu 2010-07-20
  • 打赏
  • 举报
回复
if("".equals(lastName)||"".equals(firstName)||lastName.length()==0||firstName.length()==0){
out.println("last name and first name are required!");
return;
}
值为空
a380627112 2010-07-20
  • 打赏
  • 举报
回复
不知道怎么回事了,希望高手来解答下啊
ontrackfor19888 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ontrackfor19888 的回复:]

引用 9 楼 a380627112 的回复:

在html页面上输入东西以后就不为空,打印在tomcat上可以显示名字,就是点submit提交不过去。一点就出现下载页面,弹出迅雷
说明参数已经接收过来了啊 !
[/Quote]
怎么会为空呢
ontrackfor19888 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 a380627112 的回复:]

在html页面上输入东西以后就不为空,打印在tomcat上可以显示名字,就是点submit提交不过去。一点就出现下载页面,弹出迅雷
[/Quote]说明参数已经接收过来了啊 !
ontrackfor19888 2010-07-20
  • 打赏
  • 举报
回复
response.setContentType("html/text;charset=GBK");

??
应该是text/html吧
a380627112 2010-07-20
  • 打赏
  • 举报
回复
而且数据库一直是空的,什么也没写进去
a380627112 2010-07-20
  • 打赏
  • 举报
回复
在html页面上输入东西以后就不为空,打印在tomcat上可以显示名字,就是点submit提交不过去。一点就出现下载页面,弹出迅雷
closewbq 2010-07-20
  • 打赏
  • 举报
回复
if(lastName.length()==0||firstName.length()==0){
先判断为null在进行.length()操作。
如果lastName或者firstName没有出入值的话,这肯定就是null了。
meadking 2010-07-20
  • 打赏
  • 举报
回复
500 程序问题,编译错误,或者运行异常。

if(lastName.length()==0||firstName.length()==0){
out.println("last name and first name are required!");
return;
}

你的lastName 和firstName没有获取到form里面的值。
是null
请用assert判断,或者用if
先sysout一下,肯定是这里问题
怪人伽利略 2010-07-20
  • 打赏
  • 举报
回复
路过接分的
cfd406635982 2010-07-20
  • 打赏
  • 举报
回复
你自己打印一下lastName或者firstName 看是不是为空啊、
a380627112 2010-07-20
  • 打赏
  • 举报
回复
麻烦你帮我调试下啊
a380627112 2010-07-20
  • 打赏
  • 举报
回复
第35行:if(lastName.length()==0||firstName.length()==0){
out.println("last name and first name are required!");
return;
}
在填写了lastName 和firstNmae 之后程序在tomcat里面确实运行了,但是跳不到返回的页面,数据库里面也写不进不东西。一点提交就弹出来下载啊,迅雷啊,这些东西。哎,真不知道哪里出的异常
ontrackfor19888 2010-07-20
  • 打赏
  • 举报
回复
小问题啊 空指针啊
ontrackfor19888 2010-07-20
  • 打赏
  • 举报
回复
35行有错误
java.lang.NullPointerException
com.play.RegistrationWithHttpSession.doGet(RegistrationWithHttpSession.java:35)

楼主可否值明35是哪一行啊!

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧