By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

how to delete user form record table

P: 52
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <body>
  3.  
  4.     <table width="50%" border="1" rules=ALL>
  5.     <tr><th> Username</th><th>Access Level</th><th>Delete</th></tr>
  6.     <%@ page language="java" import="java.sql.*" %>
  7.     <%
  8.     int j=0;
  9.  
  10.     Class.forName("com.mysql.jdbc.Driver");
  11.     Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/registration","root","root");
  12.     Statement st = con.createStatement();
  13.     ResultSet rs = st.executeQuery("select * from regist");
  14.     while (rs.next())
  15.     { j=j+1;%>
  16.     <tr><td align="center"><%=rs.getString(1)%></td><td align="center"><%=rs.getString(3)%></td><td><input type="checkbox" name="delete" value="yes"></tr>
  17.  
  18.     <%}
  19.  
  20.     %>
  21.     <form action="deleteuser.jsp">
  22.     <input type="hidden" name="recordcount" value="<%=j%>">
  23.     <input type="submit" name="b1" value="Delete" >
  24.     </table>
  25.     </form>
  26.     </body>
  27.     </html>
  28.  
//above is my page1.jsp
page and want to create deleteuser.jsp page but how it will create ?, how the values are fetch.
Mar 10 '12 #1

✓ answered by vishal prada

where i put this now..
Expand|Select|Wrap|Line Numbers
  1. getServletContext().getRequestDispatcher("/examples/displayResults.jsp").forward(req, res); 
  2.  

Share this Question
Share on Google+
15 Replies


10K+
P: 13,264
I see you are still ignoring the advice of separating Java code with HTML. Good luck.
Mar 10 '12 #2

P: 52
no no that's my old page where i am created one form
and now i want to create jsf action page for the above form.
Mar 10 '12 #3

P: 52
and sorry dear i was paste that old form and u feel i am neglect ur advice that's not my view.
i just want to know code for deleteuser.jsp page.
Mar 10 '12 #4

10K+
P: 13,264
Read about how to use servlets and let servlets be your controller. Submit to servlets not to other JSPs.
Mar 10 '12 #5

P: 52
i am continue trying to learn servlets but not yet succeeded do u have any web pages which easy to learn that.
Mar 10 '12 #6

10K+
P: 13,264
There is a free online version here :http://pdf.coreservlets.com/
you really should not be using JSPs until you understand how servlets work.
Mar 10 '12 #7

P: 52
index.html
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Test Form</title>
  6. </head>
  7. <body>
  8.     <form action="servlet1" method="post">
  9.         Name : <input type="text" name="userName"><br>
  10.         Email: <input type="text" name="email"><br>
  11.         <input type="submit" value="submit">
  12.     </form>
  13.  
  14. </body>
  15. </html>
and my ServletExample.java
Expand|Select|Wrap|Line Numbers
  1. package org.example02.test;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. public class ServletExample extends HttpServlet {
  11.  
  12.         protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  13.                 throws ServletException, IOException {
  14.             // TODO Auto-generated method stub
  15.  
  16.             String name = req.getParameter("userName");
  17.             String email = req.getParameter("email");
  18.             String ip = req.getRemoteAddr();
  19.  
  20.             resp.getWriter().println("<html>");
  21.             resp.getWriter().println("<head>");
  22.             resp.getWriter().println("<title>This is the response</title>");
  23.             resp.getWriter().println("</head>");
  24.             resp.getWriter().println("<body>");
  25.  
  26.             resp.getWriter().println("Your name is:" + name);
  27.             resp.getWriter().println("Your email is:" + email);
  28.             resp.getWriter().println("Your IP address is:" + ip);
  29.  
  30.             resp.getWriter().println("</body>");
  31.             resp.getWriter().println("</html>");
  32.  
  33.             super.doPost(req, resp);
  34.         }
  35.  
  36. }
  37.  
and web.xml file is.
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  3.     <display-name>Example02</display-name>
  4.  
  5.     <servlet>
  6.     <servlet-name>Test2</servlet-name>
  7.     <servlet-class>org.example02.test.ServletExample</servlet-class>
  8.     </servlet>
  9.  
  10.     <servlet-mapping>
  11.     <servlet-name>Test2</servlet-name>
  12.     <url-pattern>/servlet1</url-pattern>
  13.     </servlet-mapping>
  14.         <welcome-file-list>
  15.         <welcome-file>index.html</welcome-file>
  16.         <welcome-file>index.htm</welcome-file>
  17.         <welcome-file>index.jsp</welcome-file>
  18.         <welcome-file>default.html</welcome-file>
  19.         <welcome-file>default.htm</welcome-file>
  20.         <welcome-file>default.jsp</welcome-file>
  21.     </welcome-file-list>
  22. </web-app>
  23.  
  24.  
//but one error is occur
"The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL)."

//what this means and also i use GET method on the place of POST then also same error is coming.
what to do ?
Mar 10 '12 #8

10K+
P: 13,264
Don't to super.doPost(req, resp) and don't build the HTML in the servlet. That would be mixing Java code with HTML again. After receiving the parameters in your servlet and doing some processing just forward to a jsp that will take care of the presentation using

Expand|Select|Wrap|Line Numbers
  1.  getServletContext().getRequestDispatcher("/examples/displayResults.jsp").forward(req, res); 
  2.  
Mar 10 '12 #9

P: 52
where i put this now..
Expand|Select|Wrap|Line Numbers
  1. getServletContext().getRequestDispatcher("/examples/displayResults.jsp").forward(req, res); 
  2.  
Mar 10 '12 #10

P: 52
and u know what ?
on the internet guides the like this code tutorials which show
the servlet with html code.
then how can i learn proper way ?
Mar 10 '12 #11

10K+
P: 13,264
In your doPost method. Instead of super.doPost(req, resp);
And don't do any of the resp.getWriter().println( ...;
Let the JSP you are forwarding to deal with the presentation not the servlet.
Mar 10 '12 #12

P: 52
yahooooooooooooooo !
i got it
thanks dear........!
Mar 10 '12 #13

P: 52
this is my first servlet program which i done well with proper code.
thank you very much..!
Mar 10 '12 #14

10K+
P: 13,264
So now use that in your program. Every time you want to submit data to the server and do some database related processing then make the source JSP submit to a servlet. That servlet can then call normal Java classes which access the database. The servlet then sets the results as attributes and submits to a JSP to display the results.
That way your JSPs will be free of Java code and you have a good separation of concerns.
Mar 10 '12 #15

P: 52
ya off course you are right.
now i want to do one database related code.
let's try now...
Mar 10 '12 #16

Post your reply

Sign in to post your reply or Sign up for a free account.