Friday, March 27, 2015

form processing in jsp with get and post examples

Using JSP form processing can be one, using some basic HTTP functions, Get Method, Post Method, beside them it also uses some simple processing logics and elements served in JSP.

GET method:
Example:

JSP File (index.jsp)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <h2>Login form </h2>  
 <div>  
 <form action="get.jsp" method="GET">  
         Name <input type="text" name="full_name">  
 <br />  
         Email: <input type="text" name="email" /><br/>  
         Password: <input type="password" name="password" /><br/>  
 <input type="submit" value="Submit" />  
 </form>  
 </div>  
 </body>  
 </html>  

JSP File (get.jsp)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <center>  
 <h1>Welcome to Nirajan Ghimirey's Workshop </h1>  
 <p><b>Your Name:</b>  
 <%= request.getParameter("full_name")%>  
 </p>  
 <p><b>Your Email</b>  
 <%= request.getParameter("email")%>  
 </p>  
 <p><b>Your Password</b>  
 <%= request.getParameter("password")%>  
 </p>  
 </body>  
 </html>  

Below here is an example for post method, this example uses post method for user log in.

POST Method:

JSP File (index.jsp)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <h2> Login form </h2>  
 <div>  
 <form action="get.jsp" method="POST">  
         Name <input type="text" name="full_name">  
 <br /><br />  
         Email: <input type="text" name="email" /><br/><br />  
         Password: <input type="password" name="password" /><br/>  
 <input type="submit" value="Submit" />  
 </form>  
 </div>  
 </body>  
 </html> 

JSP File (post.jsp)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <center>  
 <h1>Welcome to Nirajan Ghimirey Workshop </h1>  
 <p><b>Your Name:</b>  
 <%= request.getParameter("full_name")%>  
 </p>  
 <p><b>Your Email</b>  
 <%= request.getParameter("email")%>  
 </p>  
 <p><b>Your Password</b>  
 <%= request.getParameter("password")%>  
 </p>  
 </body>  
 </html> 

No comments:

Post a Comment