Hi. Im having a difficult time to figure out how to connect all the claass and jsp as well as the .xml.
(I am working on a web application project, and these codes are for the registration module oly. Im using ECLIPSE EUROPA for my IDE, Apache Tomcat 6.0 for the web container, and lastly POSTGRESQL for my DBMS)
please help me !
here's my files:
(form.jsp)
<%@page contentType="text/html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html lang="true">
<link rel=StyleSheet href="myStyle.css" type="text/css">
<body>
<div align="center">
</div>
<h1 align="center"><img height="47" width="47" alt="" src="C:\Documents and Settings\Rachelle\Workspaces\MyEclipse 8.6\try2\WebRoot\WEB-INF\abiLogo.gif" style="height: 47px;"><strong> ALLIED BANK</strong></h1>
<ul class="navbar" style="top: 101px; left: 17px;">
<li><a href = "serviceRequestSystem.html"> Service Request System</a></li>
<li><a href = "requestInstructions.html"> Request Instructions</a></li>
<li><a href = "checkStatusInstructions.html"> Check Status Instructions</a></li>
<li><a href = "checkStatus.html"> Check Status</a></li>
<li><a href = "contactUs.html"> Contact Us</a></li>
</ul>
<center><b><font face="Courier New"><html:errors property="submitError"/></font></b></center>
<html:form action="doRegister" style="border: dashed; border-color: lightsilver; border-width: thin; margin-left: 4.2em; padding: 20px; width: 600px;" >
Requesting Unit : <html:text property="requestingUnit" /> <html:errors property="requestingUnit"/><br>
User Name : <html:text size="28" property="userCode" /> <html:errors property="userCode"/><br>
First Name : <html:text size="28" property="firstname" /> <html:errors property="firstname"/><br>
Last Name : <html:text size= "28" property="lastname" /> <html:errors property="lastname"/><br>
Gender : <html:select property="gender">
<html:option value="">Select Gender</html:option>
<html:option value="M">Male</html:option>
<html:option value="F">Female</html:option>
</html:select>
<html:errors property="gender"/><br>
Email : <html:text size="28" property="emailid" /> <html:errors property="emailid"/><br>
Password : <html:password size="28" property="pwd" /> <html:errors property="pwd"/><br>
Confirm Password : <html:password size="20" property="cpwd" /> <html:errors property="cpwd"/><br>
<br/>
<center><html:submit style="color:darkgreen; height:23px; width:80px;"><bean:message key="button.register"/></html:submit></center>
<br/>
<center><html:reset value="Reset Fields" style="color:purple; height:22px; width:100px;"></html:reset></center>
</html:form>
</body>
</html:html>
(UserRegisterForm.java)
package ojt;
import org.apache.struts.validator.ValidatorForm;
public class UserRegisterForm extends ValidatorForm{
private static final long serialVersionUID = 1L;
private String requestingUnit;
private String userCode;
private String emailid;
private String pwd;
private String cpwd;
private String firstname;
private String lastname;
private String gender;
private String submitError;
public String getRequestingUnit(){
return requestingUnit;
}
public void setRequestingUnit(String requestingUnit){
this.requestingUnit = requestingUnit;
}
public String getUserCode(){
return userCode;
}
public void setUserCode(String userCode){
this.userCode = userCode;
}
public String getSubmitError() {
return submitError;
}
public void setSubmitError(String submitError) {
this.submitError = submitError;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public String getCpwd() {
return cpwd;
}
public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
(ApplicationResources.properties)
button.register=Register
# Database configuration setting
db.user=postgres
db.password=admin
db.host=localhost
db.dbname=reg_app
# errors
errors.user.registration.requestingUnit= Requesting Unit cannot left empty.
errors.user.registration.userCode= User Name cannot left enmpty.
errors.user.registration.first.name=First Name cannot left empty.
errors.user.registration.last.name=Last Name cannot left empty.
errors.user.registration.pwd=Password cannot left empty.
errors.user.registration.pwd.confirm=Confirm Password cannot left empty.
errors.user.registration.pwd.match=Password did not matched.
errors.user.registration.email=Email cannot left empty.
errors.user.registration.email.valid=Enter Valid Email ID (e.g. allied_bank@gmail.com)
errors.user.registration.email.duplicate=Duplicate Email Found.
errors.user.registration.gender= Select Gender.
errors.user.registration.fail=Error found while User Registration !
errors.user.registration.success= Registration Complete! Log In Now!
(struts-config.xml)
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="registerform" type="ojt.UserRegisterForm"/>
</form-beans>
<action-mappings>
<action
path="/doRegister"
type="ojt.RegisterServlet"
name="registerform"
scope="request"
validate="true"
input="input">
<forward name="input" path="/form.jsp"/>
<forward name="success" path="/serviceRequestSystem.html"/>
<forward name="failure" path="/form.jsp"/>
</action>
</action-mappings>
<controller inputForward="true" />
<message-resources parameter="ojt.ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="true" />
</plug-in>
</struts-config>
(validation.xml)
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<formset>
<form name="registerform">
<field property="requestingUnit" depends="required">
<msg name="required" key="errors.user.registration.requestingUnit"/>
</field>
<field property = "userCode" depends = "required">
<msg name ="required" key = "errors.user.registration.userCode"/>
</field>
<field property="emailid" depends="required,email">
<msg name="required" key="errors.user.registration.email"/>
<msg name="email" key="errors.user.registration.email.valid"/>
</field>
<field property="pwd" depends="required">
<msg name="required" key="errors.user.registration.pwd"/>
</field>
<field property="cwd" depends="required,validwhen">
<msg name="required" key="errors.user.registration.pwd.confirm"/>
<msg name="validwhen" key="errors.user.registration.pwd.match" />
<var>
<var-name>test</var-name>
<var-value>(pwd==cpwd)</var-value>
</var>
</field>
<field property="firstname" depends="required">
<msg name="required" key="errors.user.registration.first.name"/>
</field>
<field property="lastname" depends="required">
<msg name="required" key="errors.user.registration.last.name"/>
</field>
<field property="gender" depends="required">
<msg name="required" key="errors.user.registration.gender"/>
</field>
<field property="submitError" depends="validwhen">
<msg name="validwhen" key="errors.user.registration.fail" />
<var>
<var-name>test</var-name>
<var-value>((emailid!=null) and ((pwd!=null) and ((cpwd!=null) and ((firstname!=null) and ((lastname!=null) and (gender!=null))))))</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>try2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>ojt.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
---------------------------------------------------------------------------------------
the above code is running and working. Now, I want to Connect the application with database. I havethe following code..
HERE:
(ConnectionString.java)
package ojt;
import java.sql.Connection;
public class ConnectionString {
public static String HOST = "jdbc:postgresql://localhost:5432/";
public static String DB_NAME = "allied";
public static String USER= "postgres";
public static String USER_PASSWORD = "admin";
public static String DRIVER = "org.postgresql.Driver";
public static Connection con = null;
}
(DatabaseManager.java)
package ojt;
import java.sql.*;
public class DatabaseManager{
PreparedStatement stmt = null;
//Open Connection of Database
public void openConnection(){
try{
Class.forName(ConnectionString.DRIVER).newInstance();
ConnectionString.con = DriverManager.getConnection(ConnectionString.HOST+ConnectionString.DB_NAME,ConnectionString.USER,ConnectionString.USER_PASSWORD);
}catch(Exception e){
e.printStackTrace();
System.out.print("Connection Error!");
}
}
//Close Connection of Database
public void closeConnection() throws SQLException{
ConnectionString.con.close();
}
//Add New User
public void addNewUser(String user_code,String last_name,String first_name,String password,String email_id){
try
{
//query to perform
String query = "Insert into enduser(" +
"user_code," +
"lastname," +
"firstname," +
"pwd," +
"email)" +
"Values(?,;?,?,?,?)";
stmt = ConnectionString.con.prepareStatement(query);
stmt.setString(1,user_code);
stmt.setString(2,last_name);
stmt.setString(3,first_name);
stmt.setString(4,password);
stmt.setString(5,email_id);
//execute query
stmt.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------
Now, here is the servlet that must be called every time the user will press submit button. but I get to have some errors still. I dont know how to fix the error.
But, without using struts i know how to work it out. :(
(RegisterServlet.java)
package ojt;
import java.sql.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class RegisterServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{
static final long serialVersionUID = 1L;
public RegisterServlet(){
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String user_code = req.getParameter("userCode");
String first_name = req.getParameter("firstname");
String last_name = req.getParameter("lastname");
String email_id = req.getParameter("emailid");
String password = req.getParameter("pwd");
DatabaseManager dmRegUser = new DatabaseManager();
dmRegUser.openConnection();
try{
dmRegUser.addNewUser(user_code,first_name,last_name,email_id,password);
req.getRequestDispatcher("regSuccess.jsp").forward(req,resp);
}catch(Exception e){
e.printStackTrace();
}
try{
dmRegUser.closeConnection();
}catch(SQLException e){
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
doGet(req,resp);
}
}
I have attached all the required codes.