Sunday 16 December 2012

HOW TO PASS PARAMETERS FROM JSP TO SERVLET?

    Hello friends today i am going to post about pass parameters from jsp to servlet.For pass a parameters we must have to follow the bellow code.

jsp code:
               <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script language="javascript">
          
        </script>
    </head>
    <body>
        <form action="jdbcservlet" method="post">    
            <label>first name</label>&nbsp;<input type="text" name="first"><br>
            <label>last name</label>&nbsp;<input type="text" name="last"><br>
            <label>mobile no</label>&nbsp;<input type="text" name="mob_no"><br>
             
            <input type="submit" value="submit">
           
            
               <input type="reset" value="reset">
          </form>
      
    </body>
</html>
servlet code:
public class jdbcservlet extends HttpServlet {

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(getServletConfig().getInitParameter("email"));
         String f_name,l_name,m_no;
        f_name=request.getParameter("first name");
        l_name=request.getParameter("last name");
        m_no=request.getParameter("mob_no");
        out.println("first name is: "+f_name);
       out.println("last name is: "+l_name);
       out.println("mobile no is: "+m_no);
      }
 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);
    }
}


    By using this code you are able to pass the parameters from jsp to servlet.Here for that we have used action property in form tag which shows where to transfer the control after click the submit type button.And method shows the by which method parameters will pass.get and post these two methods are available.
     If you face any problem in implementation in this code then mention in comment and feel free to ask any question related to java.Have a nice day....

1 comments:

  1. before move to advanced java you must know core java concepts..and you must have knowledge of c language.because without c you are not able to understand core java.if you are not aware of c language then i advised you to start from c language first after that java becomes more easier for you.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...