Skip to main content

Featured

Game Development Tutorials In Android

Game Development Tutorials In Android Hi Guys,                 We are shortly started Game Development Tutorials in Android Studio. So everyone supports me and share this blogs to your friends. Guys, At this time everyone is freely available to source code and we are provide a video for step by step game developments. you are learn free and feel free to support us. Guys, if you have any question to me, so feel free to comment us. I will try to answer all the question as long as possible.                                                     Thank's                                                                          ...

Examples of using HttpSession ?

Example of using HttpSession :


We are setting the attribute in the session scope in one servlet and getting that value from the session scope in another servlet. To set the attribute in the session scope, we have used the setAttribute() method of HttpSession interface and to get the attribute, we have used the getAttribute method.


index.html

1.    <form action="servlet1">  
2.    Name:<input type="text" name="userName"/><br/>  
3.    <input type="submit" value="go"/>  
4.    </form>  




FirstServlet.java

1.    import java.io.*;  
2.    import javax.servlet.*;  
3.    import javax.servlet.http.*;  
4.      
5.      
6.    public class FirstServlet extends HttpServlet {  
7.      
8.    public void doGet(HttpServletRequest request, HttpServletResponse response){  
9.            try{  
10.   
11.         response.setContentType("text/html");  
12.         PrintWriter out = response.getWriter();  
13.           
14.         String n=request.getParameter("userName");  
15.         out.print("Welcome "+n);  
16.           
17.         HttpSession session=request.getSession();  
18.         session.setAttribute("uname",n);  
19.   
20.         out.print("<a href='servlet2'>visit</a>");  
21.                   
22.         out.close();  
23.   
24.                 }catch(Exception e){System.out.println(e);}  
25.     }  
26.   
27. }  




SecondServlet.java

1.    import java.io.*;  
2.    import javax.servlet.*;  
3.    import javax.servlet.http.*;  
4.      
5.    public class SecondServlet extends HttpServlet {  
6.      
7.    public void doGet(HttpServletRequest request, HttpServletResponse response)  
8.            try{  
9.      
10.         response.setContentType("text/html");  
11.         PrintWriter out = response.getWriter();  
12.           
13.         HttpSession session=request.getSession(false);  
14.         String n=(String)session.getAttribute("uname");  
15.         out.print("Hello "+n);  
16.   
17.         out.close();  
18.   
19.                 }catch(Exception e){System.out.println(e);}  
20.     }  
21.       
22.   
23. }  




web.xml

1.    <web-app>  
2.      
3.    <servlet>  
4.    <servlet-name>s1</servlet-name>  
5.    <servlet-class>FirstServlet</servlet-class>  
6.    </servlet>  
7.      
8.    <servlet-mapping>  
9.    <servlet-name>s1</servlet-name>  
10. <url-pattern>/servlet1</url-pattern>  
11. </servlet-mapping>  
12.   
13. <servlet>  
14. <servlet-name>s2</servlet-name>  
15. <servlet-class>SecondServlet</servlet-class>  
16. </servlet>  
17.   
18. <servlet-mapping>  
19. <servlet-name>s2</servlet-name>  
20. <url-pattern>/servlet2</url-pattern>  
21. </servlet-mapping>  
22.   

23. </web-app>

If this information is important to you , and you want to , I will continue writing some more details of programming language, so click on the advertising, available on this page. This is motivate me for writing some more blogs.

Comments