Project View After Completion
Create login table and insert data into login table using insert command and here we are using Oracle10g database(you can also use other database)
create table login ( id number,
uname varchar2(20),
upass varchar2(20)
);
insert into login values (1,'him','him123');
insert into login values (2,'bon','bon123');
insert into login values (3,'jim','jim123');
commit;
URL Rewriting Example:
In this example we are searching all users in login table using username or password and if you want to see all users present in login table just make a click on submit button.
Then we are performing Update and delete operation on row data using URL Rewriting.
Every row has a unique id created into database table and we append that id into URL of Servlet on which we are performing the task.
<a href=UpdateServlet?id="+i+">Update</a>
<a href=DeleteServlet?id="+i+">Delete</a>
To get the Parameter value of Parameter Name(id) we use getParameter() method.
we can send multiple Parameter values append with multiple Parameter names using Ampersand(&) to separate them.<a href=Servlet?PN1=PV1&PN2=PV2&PN3=PV3>LinkName</a>
index.jsp
click on Search Link.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1><a href="Search_Servlet">Search</a></h1>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Search_Servlet</servlet-name>
<servlet-class>Search_Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateServlet</servlet-name>
<servlet-class>UpdateServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteServlet</servlet-name>
<servlet-class>DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Search_Servlet</servlet-name>
<url-pattern>/Search_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateServlet</servlet-name>
<url-pattern>/UpdateServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteServlet</servlet-name>
<url-pattern>/DeleteServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
MyDAO.java
import java.sql.*;
public class MyDAO {
public static Statement getSt()
{
Statement st=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
st=con.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
return st;
}
}
Search_Servlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class Search_Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
searchForm(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
searchForm(request, response);
out.println("<center>");
String s1=request.getParameter("t1");
try
{
Statement st=MyDAO.getSt();
ResultSet rs=st.executeQuery("select * from login where uname like '%"+s1+"%' or upass like '%"+s1+"%'");
out.println("<table border=1>");
while(rs.next())
{
int i=rs.getInt(1);
out.println("<tr>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td><a href=UpdateServlet?id="+i+">Update</a></td>");
out.println("<td><a href=DeleteServlet?id="+i+">Delete</a></td>");
out.println("</tr>");
}
out.println("</table>");
}
catch(Exception e)
{
out.println(e);
}
}
private void searchForm(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html><body>");
out.println("<form method=post>");
out.println("<center>");
out.println("<h1>Make Your Search</h1>");
out.println("<br>");
out.println("<input type=text name=t1>");
out.println("<br>");
out.println("<input type=submit value=Submit>");
out.println("</center>");
out.println("</form>");
out.println("</body></html>");
}
}
UpdateServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class UpdateServlet extends HttpServlet {
String uname,upass;
Statement st;
static String ii;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try
{
ii=request.getParameter("id");
st=MyDAO.getSt();
ResultSet i=st.executeQuery("select * from login where id='"+ii+"'");
if(i.next())
{
uname=i.getString(2);
upass=i.getString(3);
}
out.println("<html><body>");
out.println("<form method=post>");
out.println("<center>");
out.println("<h1>Update Form</h1>");
out.println("<table>");
out.println("<tr>");
out.println("<td>");
out.println("User Name : ");
out.println("</td>");
out.println("<td>");
out.println("<input type=text name=t1 value="+uname+">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("Password : ");
out.println("</td>");
out.println("<td>");
out.println("<input type=text name=t2 value="+upass+">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("<input type=submit value=Submit>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</center>");
out.println("</form>");
out.println("</body></html>");
}
catch(Exception e){out.println(e);}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String s1=request.getParameter("t1");
String s2=request.getParameter("t2");
out.println(s1+" "+s2+" "+ii);
try
{
Statement st=MyDAO.getSt();
int i=st.executeUpdate("update login set uname='"+s1+"',upass='"+s2+"' where id='"+ii+"'");
if(i!=0)
{
out.println("Updated Successfully");
}
}
catch(Exception e){out.println(e);}
}
}
DeleteServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try
{
String i=request.getParameter("id");
Statement st=MyDAO.getSt();
int j=st.executeUpdate("delete from login where id='"+i+"'");
out.println("<html><body><h1>");
out.println(j+" record(s) deleted");
out.println("</h1></body></html>");
}
catch(Exception e){out.println(e);}
}
}
Download Code Link 1
Download Code Link 1
Output:
No comments:
Post a Comment