now in this post get command from view.jsp page and if that is equal to update then get user details using that name and display those details in insertupdate.jsp page and automatically the submit button change to update button because that value is getAction. we will set that action as update in database code.
1. get username and action from view.jsp page
2.create a method getDetails in database class
3.get details in servlet and forward to insertupdate.jsp page
1.get details from view.jsp
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserBean;
import validation.ValidateForm;
import database.DBClass;
import java.util.List;
/**
*
* @author Jagadeesh
*/
public class ControllerServlet extends HttpServlet {
//declare values to get form values from jsp page
String userName;
String dateOfBirth;
String email;
String phoneNo;
String action;
UserBean bean = new UserBean();
ValidateForm validateform = new ValidateForm();
DBClass dbobject = new DBClass();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//get the values from jsp page
userName = request.getParameter("userName");
dateOfBirth = request.getParameter("dateOfBirth");
email = request.getParameter("email");
phoneNo = request.getParameter("phoneNo");
action = request.getParameter("action");
if(action.equals("submit"))
{
//set values to bean.For this call below method
setValuesToBean();
//check all form values are valid or not. send bean object
UserBean checkedbean = validateform.validateData(bean);
if(!checkedbean.getIsValid())
{
//if data is invalid.set bean object in request and pass that request to
//insertupdate.jsp using forward
request.setAttribute("error",checkedbean);
RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
//now display errors in that jsp page
}
else
{
//using DBClass object call insertDetails method and pass bean object
dbobject.insertDetails(bean);
List list = dbobject.getAlldetails();
request.setAttribute("list", list);
//forward to insertupdate page using requestdispatcher
RequestDispatcher rd= request.getRequestDispatcher("view.jsp");
//display a message to client.store message in request object
//forwarding to jsp
rd.forward(request, response);
}
}
if(action.equals("edit"))
{
//get userdetails of particular name
UserBean ubean = dbobject.getDetails(userName);
request.setAttribute("updateuser",ubean);
RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
}
}
catch(Exception e)
{
out.println(e);
}
finally {
out.close();
}
}
//this method is used to setvalues to bean
public void setValuesToBean()
{
bean.setUserName(userName);
bean.setDateOfBirth(dateOfBirth);
bean.setEmail(email);
bean.setPhoneNo(phoneNo);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
2. create a method in database class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.UserBean;
/**
*
* @author Jagadeesh
*/
public class DBClass {
public Connection createConnection() throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root");
return connection;
}
//we get values from servlet by passing bean object to insertdetails method
public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException
{
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("insert into userdetails values(?,?,?,?)");
//set values to prepared statement object by getting values from bean object
pstmt.setString(1,bb.getUserName());
pstmt.setString(2,bb.getDateOfBirth());
pstmt.setString(3,bb.getEmail());
pstmt.setString(4,bb.getPhoneNo());
int i = pstmt.executeUpdate();
return i;
}
public List getAlldetails()throws SQLException, ClassNotFoundException
{
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("select * from userdetails");
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
while(rs.next())
{
UserBean ubean = new UserBean();
ubean.setUserName(rs.getString(1));
ubean.setDateOfBirth(rs.getString(2));
ubean.setEmail(rs.getString(3));
ubean.setPhoneNo(rs.getString(4));
list.add(ubean);
}
return list;
}
public UserBean getDetails(String uname)throws SQLException, ClassNotFoundException
{
//here we will write code to get a single record from database
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?");
pstmt.setString(1, uname);
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
UserBean ubean = new UserBean();
while(rs.next())
{
ubean.setUserName(rs.getString(1));
ubean.setDateOfBirth(rs.getString(2));
ubean.setEmail(rs.getString(3));
ubean.setPhoneNo(rs.getString(4));
}
ubean.setAction("update");
return ubean;
}
/* public int UpateDetails(int uid)throws SQLException, ClassNotFoundException
{
//here we will write code to update a record
}*/
}
now display selected record in insertupdate.jsp page
<%--
Document : index
Author : Jagadeesh
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="model.UserBean" %>
<% UserBean bean;
bean = new UserBean();
//get request from servlet if data is invalid
if(request.getAttribute("error")!=null)
{
bean = (UserBean)request.getAttribute("error");
}
if(request.getAttribute("updateuser")!=null)
{
bean = (UserBean)request.getAttribute("updateuser");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="ControllerServlet">
<CENTER>
<TABLE border="0"width="600px">
<TR>
<TD width="150px">Name:</TD>
<TD>
<INPUT TYPE="text" NAME="userName" value="<%=bean.getUserName()%>">
</TD>
<TD width="350px">
<font color="red"><%=bean.getUserNameError()%> </font>
</TD>
</TR>
<TR>
<TD width="150px">Date Of Birth:</TD>
<TD>
<INPUT TYPE="text" NAME="dateOfBirth" value="<%=bean.getDateOfBirth()%>" <%=request.getAttribute("updateuser")!=null?"readonly":""%>>
</TD>
<TD>
<font color="red"><%=bean.getDateOfBirthError()%> </font>
</TD>
</TR>
<TR>
<TD width="150px">E-Mail</TD>
<TD>
<INPUT TYPE="text" NAME="email" value="<%=bean.getEmail()%>">
</TD>
<TD>
<font color="red"><%=bean.getEmailError()%> </font>
</TD>
</TR>
<TR>
<TD width="150px">Phone no:</TD>
<TD>
<INPUT TYPE="text" NAME="phoneNo" value="<%=bean.getPhoneNo()%>">
</TD>
<TD>
<font color="red"><%=bean.getPhoneNoError()%> </font>
</TD>
</TR>
<TR>
<TD colspan="2" align="center">
<INPUT TYPE="submit" value="<%=bean.getAction()%>" name="action">
</TD>
<TD>
</TD>
</TR>
</TABLE>
</CENTER>
</form>
</body>
</html>
run insertupdate.jsp page and enter somedetails and it will display all fidleda and select one record then it wil show following output with update button
in this post we got all details for selected user. in next post we will update that user details