Tuesday, 26 May 2015

Example of ServletRequest to display the name of the user

Example of ServletRequest to display the name of the user

In this example, we are displaying the name of the user in the servlet. For this purpose, we have used the getParameter method that returns the value for the given request parameter name.

index.html
1.    <form action="welcome" method="get">  
2.    Enter your name<input type="text" name="name"><br>  
3.    <input type="submit" value="login">  
4.    </form>  

DemoServ.java
1.    import javax.servlet.http.*;  
2.    import javax.servlet.*;  
3.    import java.io.*;  
4.    public class DemoServ extends HttpServlet{  
5.    public void doGet(HttpServletRequest req,HttpServletResponse res)  
6.    throws ServletException,IOException  
7.    {  
8.    res.setContentType("text/html");  
9.    PrintWriter pw=res.getWriter();  
10.   
11. String name=req.getParameter("name");//will return value  
12. pw.println("Welcome "+name);  
13.   
14. pw.close();  
15. }}  


No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...