In your application when i am trying to execute it from the below page
<%@ page language="java" import="java.util.*;"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Servlet Application</TITLE> <script language="javascript"> function editRecord(id){ window.location.href="UserServlet/"+id; } function deleteRecord(id){ window.location.href="deleteUser/"+id; } </script> </HEAD> <BODY> <br> <table align="center"> </table> <br> <table width="600px" align="center" style="background-color:#EDF6EA;border:1px solid #000000;"> <tr><td colspan=9 align="center" height="10px"></td></tr> <tr><td colspan=9 align="center"><a href="addUser.jsp" style="font-weight:bold;color:#cc0000;">Add New User</a></td></tr> <tr><td colspan=9 align="center" height="10px"></td></tr> <tr style="background-color:#7BA88B;font-weight:bold;"> <td>User Id</td><td>UserName</td><td>First Name</td> <td>Last Name</td><td>City</td><td>Sate</td> <td>Country</td><td>Edit</td><td>Delete</td> </tr> <% String bgcolor=""; int count=0; List viewList = new ArrayList(); Iterator viewItr; if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="") { List userList = (ArrayList)request.getAttribute("userList"); Iterator itr = userList.iterator(); while(itr.hasNext()) { if(count%2==0) { bgcolor = "#C8E2D1"; } else { bgcolor = "#EAF8EF"; } viewList = (ArrayList)itr.next(); int id = Integer.parseInt(viewList.get(0).toString()); viewItr = viewList.iterator(); %> <tr style="background-color:<%=bgcolor%>;"> <% while(viewItr.hasNext()) { %> <td><%=viewItr.next()%></td> <% } count++; %> <td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=id%>);" ></td> <td><input type="button" name="delete" style="background-color:#ff0000;font-weight:bold;;color:#ffffff;" value="Delete" onclick="deleteRecord(<%=id%>);"></td> </tr> <% } } if(count==0) { %> <tr><td colspan="9" align="center"> </td></tr> <tr><td colspan="9" align="center">No Record Avaliable</td></tr> <% } %> <tr><td colspan=9 align="center" height="2px"></td></tr> </table> </BODY> </HTML>
when i click on edit button it is going on follwoing servlet
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.sql.*; import java.sql.*; import java.util.*; public class UserServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String str[] = request.getRequestURI().toString().split("/"); int id = Integer.parseInt(str[3]); System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "user_register"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; // out.println(request.getRequestURI()); Statement st; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Connected to the database"); ArrayList userList=null; String query = "select * from userregister where id="+id; System.out.println("query " + query); st = conn.createStatement(); ResultSet rs = st.executeQuery(query); while(rs.next()) { userList =new ArrayList(); userList.add(rs.getInt(1)); userList.add(rs.getString(2)); userList.add(rs.getString(4)); userList.add(rs.getString(5)); userList.add(rs.getString(6)); userList.add(rs.getString(7)); userList.add(rs.getString(8)); } request.setAttribute("userList",userList); String nextJSP = "/editUser.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP); dispatcher.forward(request,response); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }
but not redicting to editUser.jsp that is following
<%@ page language="java" import="java.util.*;"%> <html> <head> </head> <body> <%! int id; String first_name=""; String last_name=""; String username=""; String city=""; String state=""; String country=""; List userList=null; %> <% if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="") { userList = (ArrayList)request.getAttribute("userList"); id=Integer.parseInt(userList.get(0).toString()); username=userList.get(1).toString(); first_name=userList.get(2).toString(); last_name=userList.get(3).toString(); city=userList.get(4).toString(); state=userList.get(5).toString(); country=userList.get(6).toString(); //out.println(id); } %> <form name="userform" method="post" action="../editUser"> <br><br><br> <table align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;"> <input type="hidden" name="id" value="<%=id%>"> <tr><td colspan=2 style="font-weight:bold;" align="center">Edit User</td></tr> <tr><td colspan=2 align="center" height="10px"></td></tr> <tr> <td>First Name</td> <td><input type="text" name="first_name" value="<%=first_name%>"></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="last_name" value="<%=last_name%>"></td> </tr> <tr> <td>UserName</td> <td><input type="text" name="username" value="<%=username%>"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" value=""></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" value="<%=city%>"></td> </tr> <tr> <td>State</td> <td><input type="text" name="state" value="<%=state%>"></td> </tr> <tr> <td>Country</td> <td><input type="text" name="country" value="<%=country%>"></td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <tr><td colspan=2 align="center" height="10px"></td></tr> </table> </form> </body> </html>
please help me
We are providing you another application. Check it.
1)application.jsp:
<%@ page import="java.sql.*" %> <html> <head> <script language="javascript"> function editRecord(id){ var f=document.form; f.method="post"; f.action='edit.jsp?id='+id; f.submit(); } function deleteRecord(id){ var f=document.form; f.method="post"; f.action='delete.jsp?id='+id; f.submit(); } </script> </head> <body> <br><br> <form method="post" name="form"> <table border="1"> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "test"; String driver = "com.mysql.jdbc.Driver"; String userName ="root"; String password="root"; int sumcount=0; Statement st; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db,userName,password); String query = "select * from employee"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td> <td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td> <td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> </form> </body> </html>
2)edit.jsp:
<%@page language="java"%> <%@page import="java.sql.*"%> <form method="post" action="update.jsp"> <table border="1"> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% String id=request.getParameter("id"); int no=Integer.parseInt(id); int sumcount=0; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); String query = "select * from employee where id='"+no+"'"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); while(rs.next()){ %> <tr> <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td> <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td> <td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td> <td><input type="text" name="email" value="<%=rs.getString("email")%>"></td> <td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td> </tr> <tr> <td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <% } } catch(Exception e){} %> </table> </form>
continue..
3)update.jsp:
<%@page import="java.sql.*"%> <% String ide=request.getParameter("id"); int num=Integer.parseInt(ide); String name=request.getParameter("name"); String address=request.getParameter("address"); int contact=Integer.parseInt(request.getParameter("contact")); String email=request.getParameter("email"); try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); Statement st=null; st=conn.createStatement(); st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'"); response.sendRedirect("/examples/jsp/application.jsp"); } catch(Exception e){ System.out.println(e); } %>
4)delete.jsp:
<%@page import="java.sql.*"%> <% String id=request.getParameter("id"); int no=Integer.parseInt(id); int sumcount=0; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st = conn.createStatement(); st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'"); response.sendRedirect("application.jsp"); } catch(Exception e){} %>
thanx for your help but i need in servlet form as there is lots of other functionality which i have to embed in it..
can you help me with this..
just tell me the way i can extract data from the database in jsp form field but logic i have to apply to apply in servlets only for extraction of database fields and in jsp just i have to print the values extracted through servlet...
Ads