hello friends
i am new to struts.
i want to add the user information in the database and then display it back to him.
i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5..
i have created a form bean to get the data from the user.
then in my action class i m trying to get a database connection and adding the values of the user. and i also wanna show them back to him.
its a dummy project for my learning(to get an insight into struts)
the code is as follows.
m sure the mistake is a silly one but as of now i am lost.
please help
Code:
register.jsp
RegisterForm.ja va
RegisterAction. java
RegisterDAO.jav a
User.java
Also the database is named carsite and it has a table register created in it.
the struts-config and web xml enteries are error free. i shall post them too if need be.
please help
the error is:
WARNING: Unhandled Exception thrown: class java.lang.NullP ointerException
Aug 4, 2009 10:28:14 AM org.apache.cata lina.core.Stand ardWrapperValve invoke
SEVERE: Servlet.service () for servlet action threw exception
java.lang.NullP ointerException
at register.Regist erDAO.setRecord (RegisterDAO.ja va:76)
at register.Regist erAction.execut e(RegisterActio n.java:42)
at org.apache.stru ts.action.Reque stProcessor.pro cessActionPerfo rm(RequestProce ssor.java:421)
at org.apache.stru ts.action.Reque stProcessor.pro cess(RequestPro cessor.java:226 )
at org.apache.stru ts.action.Actio nServlet.proces s(ActionServlet .java:1164)
at org.apache.stru ts.action.Actio nServlet.doPost (ActionServlet. java:415)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:709)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
at org.apache.cata lina.core.Appli cationFilterCha in.internalDoFi lter(Applicatio nFilterChain.ja va:252)
at org.apache.cata lina.core.Appli cationFilterCha in.doFilter(App licationFilterC hain.java:173)
at org.apache.cata lina.core.Stand ardWrapperValve .invoke(Standar dWrapperValve.j ava:213)
at org.apache.cata lina.core.Stand ardContextValve .invoke(Standar dContextValve.j ava:178)
at org.apache.cata lina.core.Stand ardHostValve.in voke(StandardHo stValve.java:12 6)
at org.apache.cata lina.valves.Err orReportValve.i nvoke(ErrorRepo rtValve.java:10 5)
at org.apache.cata lina.core.Stand ardEngineValve. invoke(Standard EngineValve.jav a:107)
at org.apache.cata lina.connector. CoyoteAdapter.s ervice(CoyoteAd apter.java:148)
at org.apache.coyo te.http11.Http1 1Processor.proc ess(Http11Proce ssor.java:869)
at org.apache.coyo te.http11.Http1 1BaseProtocol$H ttp11Connection Handler.process Connection(Http 11BaseProtocol. java:664)
at org.apache.tomc at.util.net.Poo lTcpEndpoint.pr ocessSocket(Poo lTcpEndpoint.ja va:527)
at org.apache.tomc at.util.net.Lea derFollowerWork erThread.runIt( LeaderFollowerW orkerThread.jav a:80)
at org.apache.tomc at.util.threads .ThreadPool$Con trolRunnable.ru n(ThreadPool.ja va:684)
at java.lang.Threa d.run(Unknown Source)
i am new to struts.
i want to add the user information in the database and then display it back to him.
i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5..
i have created a form bean to get the data from the user.
then in my action class i m trying to get a database connection and adding the values of the user. and i also wanna show them back to him.
its a dummy project for my learning(to get an insight into struts)
the code is as follows.
m sure the mistake is a silly one but as of now i am lost.
please help
Code:
register.jsp
Code:
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<link href="example.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top">
<table border="0">
<tr>
<td width="250">
<img src="car.jpg"/> </td>
<td width="250"> <h1 align="center"><font face="courier new" >thecarsite</font></h1> </td>
<td width="250"> <h2 align="right"><html:link forward="index">Home</html:link></h2></td>
</tr>
</table>
<hr width="100%">
</div>
<div id="restpage" >
<html:errors/>
<html:form action="/register">
<h2>USER DETAILS:</h2>
<font size="-1" color="red">*</font>FIRST NAME <html:text property="fname" size="15" maxlength="20"/>
MIDDLE NAME <html:text property="mname" size="10" maxlength="20"/>
<font size="-1" color="red">*</font>LAST NAME <html:text property="lname" size="15" maxlength="20"/><br><br>
DATE OF BIRTH <html:text property="dob" size="10" maxlength="10"/>(dd/mm/yyyy)<br><br>
ADDRESS <html:text property="add" size="30" maxlength="50"/>
CITY <html:text property="city" size="15" maxlength="20"/>
STATE <html:text property="state" size="15" maxlength="20"/><br><br>
ZIP <html:text property="zip" size="6" maxlength="6"/>
CONTACT NO <html:text property="cno" size="10" maxlength="10"/>
<font size="-1" color="red">*</font>EMAIL ID <html:text property="email" size="15" maxlength="20"/><br>
<h2>LOGIN DETAILS</h2>
<font size="-1" color="red">*</font>USER NAME
<html:text property="uid" size="15" maxlength="20"/><br>
<font size="-1" color="red">*</font>PASSWORD
<html:password property="psswd" size="15" maxlength="20"/><br>
<font size="-1" color="red">*</font>RE-TYPE PASSWORD <html:password property="rpsswd" size="15" maxlength="20"/><br><br><br>
<center>
<font size="5" color="red">(*) Mandatory Fields</font><br><br>
</center>
<html:submit value="Submit" /><br>
</html:form >
</div>
</body>
</html>
RegisterForm.ja va
Code:
package register;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class RegisterForm extends ActionForm {
String Fname,Lname,Email,Uid,Psswd,Rpsswd,mname,dob,add,cno,state,city,zip;
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
Lname = lname;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getUid() {
return Uid;
}
public void setUid(String uid) {
Uid = uid;
}
public String getPsswd() {
return Psswd;
}
public void setPsswd(String psswd) {
Psswd = psswd;
}
public String getRpsswd() {
return Rpsswd;
}
public void setRpsswd(String rpsswd) {
Rpsswd = rpsswd;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
ActionErrors actionErrors= new ActionErrors();
if(getFname()=="")
{
actionErrors.add("Fname", new ActionMessage("fname.required"));
}
if(getLname()=="")
{
actionErrors.add("Lname", new ActionMessage("lname.required"));
}
if(getEmail()=="")
{
actionErrors.add("Email", new ActionMessage("email.required"));
}
if(getUid()=="")
{
actionErrors.add("Username",new ActionMessage("uid.required"));
}
if(getPsswd()=="")
{
actionErrors.add("Password",new ActionMessage("psswd.required"));
}
else if (getUid().equals(getPsswd()))
{
actionErrors.add("Unp",new ActionMessage("value.match"));
}
if(getRpsswd()=="")
{
actionErrors.add("Password",new ActionMessage("rpsswd.required"));
}
if ( getRpsswd().equals(getPsswd()) )
{}else{
actionErrors.add("Unp",new ActionMessage("value.notmatch"));
}
return actionErrors;
}
}
Code:
package register;
import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import register.RegisterDAO;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class RegisterAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
RegisterForm form1= (RegisterForm)form;
RegisterDAO registerDAO = new RegisterDAO();
User user = new User();
Connection con = null;
DataSource ds = null;
ds = getDataSource(request);
String fname= form1.getFname();
String mname= form1.getMname();
String lname= form1.getLname();
String dob= form1.getDob();
String address= form1.getAdd();
String city= form1.getCity();
String state= form1.getState();
String zip= form1.getZip();
String cno= form1.getCno();
String email= form1.getEmail();
String uid= form1.getUid();
String psswd= form1.getPsswd();
user= RegisterDAO.setRecord(con,user);
user= RegisterDAO.getRecord(con,user);
registerDAO.closeConnection(con);
return mapping.findForward("success");
}
}
Code:
package register;
import register.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.sql.DataSource;
public class RegisterDAO {
public Connection getConnection(DataSource carsite) {
Connection con = null;
DataSource ds = carsite;
try {
con = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public void closeConnection(Connection con) {
try {
con.setAutoCommit(true);
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static User getRecord(Connection con, User user) {
ArrayList<User> list= new ArrayList<User>();
String qry = "select * from register where username=?";
ResultSet resultSet = null;
try {
PreparedStatement pstat = con.prepareStatement(qry);
pstat.setString(11, user.getUid());
resultSet = pstat.executeQuery();
while (resultSet.next()) {
user.setFname(resultSet.getString("fname"));
user.setMname(resultSet.getString("mname"));
user.setLname(resultSet.getString("lname"));
user.setDob (resultSet.getString("dob"));
user.setAdd (resultSet.getString("address"));
user.setCity (resultSet.getString("city"));
user.setState(resultSet.getString("state"));
user.setFname(resultSet.getString("zip"));
user.setFname(resultSet.getString("cno"));
user.setFname(resultSet.getString("email"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} return user;
}
public static User setRecord(Connection con, User user) {
String qry = "INSERT INTO register VALUES('" + user.getFname() + "','"
+ user.getMname() + "','" + user.getLname() + "' ,'" + user.getDob() + " ','" + user.getAdd()
+ "','" + user.getState() + "' ,'" + user.getZip() + "','" + user.getCno() + "','"
+ user.getEmail() + "','" + user.getUid() + "','" + user.getPsswd() + "')";
ResultSet resultSet = null;
try {
// m yet to write the code for this
} }
catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
Code:
package register;
public class User {
private String Fname,Lname,Email,Rpsswd,mname,dob,add,cno,state,city,zip,uid,psswd;
public String getPsswd() {
return psswd;
}
public void setPsswd(String psswd) {
this.psswd = psswd;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
Lname = lname;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getRpsswd() {
return Rpsswd;
}
public void setRpsswd(String rpsswd) {
Rpsswd = rpsswd;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
the struts-config and web xml enteries are error free. i shall post them too if need be.
please help
the error is:
WARNING: Unhandled Exception thrown: class java.lang.NullP ointerException
Aug 4, 2009 10:28:14 AM org.apache.cata lina.core.Stand ardWrapperValve invoke
SEVERE: Servlet.service () for servlet action threw exception
java.lang.NullP ointerException
at register.Regist erDAO.setRecord (RegisterDAO.ja va:76)
at register.Regist erAction.execut e(RegisterActio n.java:42)
at org.apache.stru ts.action.Reque stProcessor.pro cessActionPerfo rm(RequestProce ssor.java:421)
at org.apache.stru ts.action.Reque stProcessor.pro cess(RequestPro cessor.java:226 )
at org.apache.stru ts.action.Actio nServlet.proces s(ActionServlet .java:1164)
at org.apache.stru ts.action.Actio nServlet.doPost (ActionServlet. java:415)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:709)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
at org.apache.cata lina.core.Appli cationFilterCha in.internalDoFi lter(Applicatio nFilterChain.ja va:252)
at org.apache.cata lina.core.Appli cationFilterCha in.doFilter(App licationFilterC hain.java:173)
at org.apache.cata lina.core.Stand ardWrapperValve .invoke(Standar dWrapperValve.j ava:213)
at org.apache.cata lina.core.Stand ardContextValve .invoke(Standar dContextValve.j ava:178)
at org.apache.cata lina.core.Stand ardHostValve.in voke(StandardHo stValve.java:12 6)
at org.apache.cata lina.valves.Err orReportValve.i nvoke(ErrorRepo rtValve.java:10 5)
at org.apache.cata lina.core.Stand ardEngineValve. invoke(Standard EngineValve.jav a:107)
at org.apache.cata lina.connector. CoyoteAdapter.s ervice(CoyoteAd apter.java:148)
at org.apache.coyo te.http11.Http1 1Processor.proc ess(Http11Proce ssor.java:869)
at org.apache.coyo te.http11.Http1 1BaseProtocol$H ttp11Connection Handler.process Connection(Http 11BaseProtocol. java:664)
at org.apache.tomc at.util.net.Poo lTcpEndpoint.pr ocessSocket(Poo lTcpEndpoint.ja va:527)
at org.apache.tomc at.util.net.Lea derFollowerWork erThread.runIt( LeaderFollowerW orkerThread.jav a:80)
at org.apache.tomc at.util.threads .ThreadPool$Con trolRunnable.ru n(ThreadPool.ja va:684)
at java.lang.Threa d.run(Unknown Source)
Comment