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

Example program of ServletContext to set and get attribute

Question : Example of ServletContext to set and get attribute?

Ans : we are setting the attribute in the application scope and getting that value from another servlet.


Servlet1.java
1.     import java.io.*;  
2.     import javax.servlet.*;  
3.     import javax.servlet.http.*;  
4.       
5.       
6.     public class Servlet1 extends HttpServlet{  
7.     public void doGet(HttpServletRequest req,HttpServletResponse res)  
8.     {  
9.     try{  
10.     
11.   res.setContentType("text/html");  
12.   PrintWriter out=res.getWriter();  
13.     
14.   ServletContext context=getServletContext();  
15.   context.setAttribute("company","IBM");  
16.     
17.   out.println("Welcome to first servlet");  
18.   out.println("<a href='servlet2'>visit</a>");  
19.   out.close();  
20.     
21.   }catch(Exception e){out.println(e);}  
22.     
23.   }}  


Servlet2.java
1.     import java.io.*;  
2.     import javax.servlet.*;  
3.     import javax.servlet.http.*;  
4.       
5.       
6.     public class Servlet2 extends HttpServlet{  
7.     public void doGet(HttpServletRequest req,HttpServletResponse res)  
8.     {  
9.     try{  
10.     
11.   res.setContentType("text/html");  
12.   PrintWriter out=res.getWriter();  
13.     
14.   ServletContext context=getServletContext();  
15.   String n=(String)context.getAttribute("company");  
16.     
17.   out.println("Welcome to "+n);  
18.   out.close();  
19.     
20.   }catch(Exception e){out.println(e);}  
21.     
22.   }}  

web.xml
1.     <web-app>  
2.       
3.     <servlet>  
4.     <servlet-name>s1</servlet-name>  
5.     <servlet-class>Servlet1</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>Servlet2</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