Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I need some help with my Servlet

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question I need some help with my Servlet

    Hello, iam a begginer by using servlets..

    Iam doing a small project which connects to a Sql DB.
    Iam using 1 doGet + 1 doPost method + 1 HtmOutputForm (inside doPost)
    I have 2 buttons.
    When i click on the 1st button the data from the TextFields are saved into my DB.
    But when i click the 2nd button it does the same thing like the 1st Button.. but i want it to delete the data from my DB.
    If iam right i must add an "IF statement" in the doPost Function to check which button is cliked ?
    Can u help?

    Thanks in advance.



    A piece of my code
    -------------------


     
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    			throws ServletException, IOException 
    		{
    		Connection dbCon;
     
    		req.setCharacterEncoding("utf-8");
     
                    ...................
                    ...................
     
    		try 
    		{
    			Class.forName(driver);
    			dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");	
     
     
    			PreparedStatement st = dbCon.prepareStatement("INSERT INTO Students (id, fname, lname, age, email) VALUES (? ,? ,? ,?, ?)");			
    			st.setString(1, "id");
    			st.setString(2, "fname");
    			st.setString(3, "lname");
    			st.setString(4, "email");
    			st.setString(5, "age");
     
    			int j = st.executeUpdate(); 
     
    			res.sendRedirect("ShowTable"); //doGet
     
    		} catch (Exception e) 
    		{
    			res.sendRedirect("ShowTable?errormsg=" + e.getMessage());
    		}
    	}
     
    	void printForm1(PrintWriter out) 
    	{
                    out.println("<b> id: </b> <input type=\"text\" name=\"id\" ><br>");
    		out.println("<form action=\"ShowTable\" method=\"POST\">");
    		out.println("<b> fname :  </b> <input type=\"text\" name=\"fname\" ><br>");
    		out.println("<b> lname :  </b> <input type=\"text\" name=\"lname\" ><br>");
    		out.println("<b> age: </b>  <input type=\"text\" name=\"age\" ><br>");
    		out.println("<b> email: </b> <input type=\"text\" name=\"email\" ><br>");
    		out.println("<input type=\"submit\"  value=\"Save Data\"> ");
    		out.println("<input type=\"submit\"  name=\"del\" value=\"Delete Data\"> ");
    		out.println("</form>");
    	}
     
     
    	void printAnyError(PrintWriter out, HttpServletRequest req) 
    	{
    		String errorMessage = req.getParameter("errormsg");
    		if (errorMessage != null) 
    	   	  out.println("<br><strong style=\"color:red\"> Error: " + errorMessage + "</strong>");
     
    	}


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I need some help with my Servlet

    Quote Originally Posted by Dcoder View Post
    If iam right i must add an "IF statement" in the doPost Function to check which button is cliked ?
    Test the presence of the parameter named "del" using getParameter of HttpServletRequest.
    If the value of parameter "del" is present (not null) it means that you have pressed the "Delete Data" button.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    Dcoder (December 8th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need some help with my Servlet

    OK i solved it .. Thanks

Similar Threads

  1. maven Application dosen't work , error: Servlet.init() for servlet
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2013, 04:12 PM
  2. send and receive byte array form servlet to another servlet via url
    By diga88 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 11th, 2013, 09:40 PM
  3. Replies: 0
    Last Post: March 11th, 2012, 04:57 PM
  4. servlet
    By selva in forum Java Servlet
    Replies: 2
    Last Post: August 7th, 2011, 10:59 PM
  5. collecting information/moving from servlet to servlet
    By CBird in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 07:04 PM