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....
Reactions:

2 comments:

  1. I am basically not a programmer and I am comparatively new to Java technology , so I was wondering what all topics should be covered up if i have to start java from the start and has any one
    studied or got any info regarding this 6 week java training online course http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate and should we also have knowledge of C language before we further move on to Advance Java topics??

    ReplyDelete
  2. 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...