Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 5 April 2014

Servlet – Attribute in Application Scope using ServletContext

In this tutorial we are going to see about , how to set and get attributes in application scope using ServletContext.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create following Servlet as shown in figure.
  • ServletContextSetAttribute
  • ServletContextGetAttribute
Create Servlet
http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.
http servlet Services
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
1.<servlet>
2.    <servlet-name>Your Servlet Name</servlet-name>
3.    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
4.  </servlet>
5.  <servlet-mapping>
6.    <servlet-name>Your Servlet Name</servlet-name>
7.    <url-pattern>/URL to Call Servlet</url-pattern>
8.  </servlet-mapping>

web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
05. id="WebApp_ID" version="3.0">
06. <display-name>ServetExample</display-name>
07. <welcome-file-list>
08.  <welcome-file>index.html</welcome-file>
09.  <welcome-file>index.htm</welcome-file>
10.  <welcome-file>index.jsp</welcome-file>
11.  <welcome-file>default.html</welcome-file>
12.  <welcome-file>default.htm</welcome-file>
13.  <welcome-file>default.jsp</welcome-file>
14. </welcome-file-list>
15. <servlet>
16.  <servlet-name>ServletContextGetAttribute</servlet-name>
17.  <servlet-class>com.javatutorialscorner.servlet.ServletContextGetAttribute</servlet-class>
18. </servlet>
19. 
20. <servlet-mapping>
21.  <servlet-name>ServletContextGetAttribute</servlet-name>
22.  <url-pattern>/ServletContextGetAttribute</url-pattern>
23. </servlet-mapping>
24. <servlet>
25.  <servlet-name>ServletContextSetAttribute</servlet-name>
26.  <servlet-class>com.javatutorialscorner.servlet.ServletContextSetAttribute</servlet-class>
27. </servlet>
28. 
29. <servlet-mapping>
30.  <servlet-name>ServletContextSetAttribute</servlet-name>
31.  <url-pattern>/ServletContextSetAttribute</url-pattern>
32. </servlet-mapping>
33.</web-app>

8. Add the required code inside doGet() method of both servlets.

ServletContextSetAttribute.java

01.package com.javatutorialscorner.servlet;
02. 
03.import java.io.IOException;
04.import java.io.PrintWriter;
05. 
06.import javax.servlet.ServletContext;
07.import javax.servlet.ServletException;
08.import javax.servlet.http.HttpServlet;
09.import javax.servlet.http.HttpServletRequest;
10.import javax.servlet.http.HttpServletResponse;
11. 
12./**
13. * Servlet implementation class ServletContextSetAttribute
14. */
15. 
16.public class ServletContextSetAttribute extends HttpServlet {
17. private static final long serialVersionUID = 1L;
18. 
19. /**
20.  * @see HttpServlet#HttpServlet()
21.  */
22. public ServletContextSetAttribute() {
23.  super();
24.  // TODO Auto-generated constructor stub
25. }
26. 
27. /**
28.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
29.  *      response)
30.  */
31. protected void doGet(HttpServletRequest request,
32.   HttpServletResponse response) throws ServletException, IOException {
33.  // TODO Auto-generated method stub
34.  ServletContext servletContext = getServletContext();
35.  servletContext.setAttribute("Tutorial", "Servlet");
36.  response.setContentType("text/html");
37.  PrintWriter writer = response.getWriter();
38.  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
39.    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet - Servlet Context Attribute</title>"
40.    + "</head><body><h1>Java Tutorials Corner - Servlet - Servlet Context Attribute</h1>
41."
42.    + "<a href=\"ServletContextGetAttribute\">Servlet Tutorial</a></body></html>");
43. }
44. 
45. /**
46.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
47.  *      response)
48.  */
49. protected void doPost(HttpServletRequest request,
50.   HttpServletResponse response) throws ServletException, IOException {
51.  // TODO Auto-generated method stub
52. }
53. 
54.}

ServletContextGetAttribute.java

01.package com.javatutorialscorner.servlet;
02. 
03.import java.io.IOException;
04.import java.io.PrintWriter;
05. 
06.import javax.servlet.ServletContext;
07.import javax.servlet.ServletException;
08.import javax.servlet.annotation.WebServlet;
09.import javax.servlet.http.HttpServlet;
10.import javax.servlet.http.HttpServletRequest;
11.import javax.servlet.http.HttpServletResponse;
12. 
13./**
14. * Servlet implementation class ServletContextGetAttribute
15. */
16.public class ServletContextGetAttribute extends HttpServlet {
17. private static final long serialVersionUID = 1L;
18. 
19. /**
20.  * @see HttpServlet#HttpServlet()
21.  */
22. public ServletContextGetAttribute() {
23.  super();
24.  // TODO Auto-generated constructor stub
25. }
26. 
27. /**
28.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
29.  *      response)
30.  */
31. protected void doGet(HttpServletRequest request,
32.   HttpServletResponse response) throws ServletException, IOException {
33.  // TODO Auto-generated method stub
34.  ServletContext servletContext = getServletContext();
35.  String contextAttribute = (String) servletContext
36.    .getAttribute("Tutorial");
37.  response.setContentType("text/html");
38.  PrintWriter writer = response.getWriter();
39.  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
40.    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet - Servlet Context Attribute</title>"
41.    + "</head><body><h1>Java Tutorials Corner - Servlet - Servlet Context Attribute</h1><table><tr>"
42.    + "<td> Context Attribute : </td><td>"
43.    + contextAttribute
44.    + "</td></tr></table></body></html>");
45. }
46. 
47. /**
48.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
49.  *      response)
50.  */
51. protected void doPost(HttpServletRequest request,
52.   HttpServletResponse response) throws ServletException, IOException {
53.  // TODO Auto-generated method stub
54. }
55. 
56.}

9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

10.call the URL which is mapped in web.xml.

http://localhost:8080/ServletExample/ServletContextSetAttribute

Output

image Click the link

image

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Servlet – Load On Startup In this tutorial we are going to see about Servlet Load on Startup with example program. Load On Startup The <load-on-startup> is the sub element of <servlet> , which is used to start the Sevlet at the time of Se… Read More
  • Servlet – RequestDispatcher Interface In this tutorial we are going to see about Servlet RequestDispatcher Interface with example program. Request Dispatcher public interface RequestDispatcher Defines an object that receives requests from the client and sends … Read More
  • Servlet – Read check box value In this tutorial we are going to see about submit form with check box and read the check box value in Servlet. Step by step instruction given below. 1. Create new Dynamic web project by choosing File –> New –> Dynamic … Read More
  • Servlet – Post method with exampleIn this tutorial we are going to see about Servlet Post method with example program.protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // code block }The do… Read More
  • Servlet – ServletConfig Interface In this tutorial we are going to see about ServletConfig interface with example program. A servlet configuration object used by a servlet container to pass information to a servlet during initialization. Methods in ServletC… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – Attribute in Application Scope using ServletContext Rating: 5 Reviewed By: eHowToNow