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
Expand|Select|Wrap|Line Numbers
- <%@ 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.java
Expand|Select|Wrap|Line Numbers
- 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;
- }
- }
Expand|Select|Wrap|Line Numbers
- 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");
- }
- }
Expand|Select|Wrap|Line Numbers
- 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;
- }
- }
Expand|Select|Wrap|Line Numbers
- 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.NullPointerException
Aug 4, 2009 10:28:14 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at register.RegisterDAO.setRecord(RegisterDAO.java:76 )
at register.RegisterAction.execute(RegisterAction.jav a:42)
at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)