JSP Scopes Example

Click here to download eclipse supported ZIP file




 

    
package com.cv.jsp.scopes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class MyServlet
 
 *  @author Chandra Vardhan
 
 */
public class TestServlet extends HttpServlet {
  
  private static final Logger LOGGER = Logger.getLogger(TestServlet.class);

  private static final long serialVersionUID = 1L;

  /**
   @see HttpServlet#HttpServlet()
   */
  public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doGet(HttpServletRequest ,HttpServletResponse ) of TestServlet class... ");
    
    response.setContentType("text/html");
    String userName = request.getParameter("userName");
    String city = request.getParameter("city");
    String gender = request.getParameter("gender");
    
    LOGGER.info("You have entered the username as : " + userName);
    LOGGER.info("You have entered the city as : " + city);
    LOGGER.info("You have entered the gender as : " + gender);
    if (userName != null && userName.length() 1) {
      request.getSession().setAttribute("userName", userName);
      getServletContext().setAttribute("city", city);
      request.setAttribute("gender", gender);
      RequestDispatcher dispatcher = request.getRequestDispatcher("jspscopes.jsp");
      dispatcher.forward(request, response);
    else {
      LOGGER.info("You have entered INVALID username. PLEASE enter username as  : chandra ");
      RequestDispatcher dispatcher = request.getRequestDispatcher("start.html");
      dispatcher.forward(request, response);
    }

  }

  /**
   @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doPost(HttpServletRequest ,HttpServletResponse ) of TestServlet class... ");
    doGet(request, response);
  }

}





<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Integer intt = new Integer(10);
pageContext.setAttribute("pageScope", intt);
%>
<%
String gender = (String) request.getAttribute("gender");
pageContext.setAttribute("requestScope", gender, PageContext.REQUEST_SCOPE);
%>
<%
String uname = (String) session.getAttribute("userName");
pageContext.setAttribute("sessionScope", uname, PageContext.SESSION_SCOPE);
%>
<%
String city = (String) application.getAttribute("city");
pageContext.setAttribute("applicationScope", city, PageContext.APPLICATION_SCOPE);
%>
<c:set var="pageScopeVar" value="I_AM_FROM_PAGE_SCOPE" scope="page" />
<c:set var="requestScopeVar" value="I_AM_FROM_REQUEST_SCOPE"
scope="request" />
<c:set var="sessionScopeVar" value="I_AM_FROM_SESSION_SCOPE"
scope="session" />
<c:set var="applicationScopeVar" value="I_AM_FROM_APPLICATION_SCOPE"
scope="application" />
<br>
<br>
<center>
<a href="scopetest.jsp">Click here to move to scope test page!!!</a>
</center>
<br>
<br>
<fieldset>
<legend>First Result page ...</legend>
<table align="center">
<tr>
<td><label>I am from
pageContext.getAttribute('pageScope') : </label></td>
<td><font color="RED"><%=pageContext.getAttribute("pageScope")%></font></td>
</tr>
<tr>
<td><label>I am from
request.getAttribute('requestScope') : </label></td>
<td><font color="RED"><%=request.getAttribute("requestScope")%></font></td>
</tr>
<tr>
<td><label>I am from
request.getSession().getAttribute('sessionScope') : </label></td>
<td><font color="RED"><%=request.getSession().getAttribute("sessionScope")%></font></td>
</tr>
<tr>
<td><label>I am from
application.getAttribute('applicationScope') : </label></td>
<td><font color="RED"><%=application.getAttribute("applicationScope")%></font></td>
</tr>
<tr>
<td><label>I am from pageScopeVar : </label></td>
<td><font color="RED"> <c:out value="${pageScopeVar}" /></font></td>
</tr>
<tr>
<td><label>I am from requestScopeVar : </label></td>
<td><font color="RED"> <c:out value="${requestScopeVar}" /></font></td>
</tr>
<tr>
<td><label>I am from sessionScopeVar : </label></td>
<td><font color="RED"> <c:out value="${sessionScopeVar}" /></font></td>
</tr>
<tr>
<td><label>I am from applicationScopeVar : </label></td>
<td><font color="RED"> <c:out
value="${applicationScopeVar}" /></font></td>
</tr>
</table>
</fieldset>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("requestScope2", "requestScope2", PageContext.REQUEST_SCOPE);
%>
<c:set var="requestScopeVar2"
value="I_AM_FROM_REQUEST_SCOPE_SET_IN_SCOPE_TEST_PAGE" scope="request" />

<jsp:forward page="requestscopetest2.jsp"></jsp:forward>  
</body>
</html>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<br>
<fieldset>
<legend>Third Result page...</legend>
<table align="center">
<tr>
<td><label>I am from
request.getAttribute('requestScope2') : </label></td>
<td><font color="RED"><%=request.getAttribute("requestScope2")%></font></td>
</tr>
<tr>
<td><label>I am from
request.getSession().getAttribute('sessionScope') : </label></td>
<td><font color="RED"><%=request.getSession().getAttribute("sessionScope")%></font></td>
</tr>
<tr>
<td><label>I am from
application.getAttribute('applicationScope') : </label></td>
<td><font color="RED"><%=application.getAttribute("applicationScope")%></font></td>
</tr>
<tr>
<td><label>I am from requestScopeVar2 : </label></td>
<td><font color="RED"> <c:out
value="${requestScopeVar2}" /></font></td>
</tr>
<tr>
<td><label>I am from sessionScopeVar : </label></td>
<td><font color="RED"> <c:out value="${sessionScopeVar}" /></font></td>
</tr>
<tr>
<td><label>I am from applicationScopeVar : </label></td>
<td><font color="RED"> <c:out
value="${applicationScopeVar}" /></font></td>
</tr>
</table>
</fieldset>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<br>
<center>
<a href="requestscopetest.jsp">Click here to move to request scope test page!!!</a>
</center>
<br>
<br>
<fieldset>
<legend>Second Result page...</legend>
<table align="center">
<tr>
<td><label>I am from pageContext.getAttribute('pageScope') :
</label></td>
<td><font color="RED"><%=pageContext.getAttribute("pageScope")%></font></td>
</tr>
<tr>
<td><label>I am from request.getAttribute('requestScope') : </label></td>
<td><font color="RED"><%=request.getAttribute("requestScope")%></font></td>
</tr>
<tr>
<td><label>I am from
request.getSession().getAttribute('sessionScope') : </label></td>
<td><font color="RED"><%=request.getSession().getAttribute("sessionScope")%></font></td>
</tr>
<tr>
<td><label>I am from application.getAttribute('applicationScope') :
</label></td>
<td><font color="RED"><%=application.getAttribute("applicationScope")%></font></td>
</tr>


<tr>
<td><label>I am from pageScopeVar :
</label></td>
<td><font color="RED"> <c:out value="${pageScopeVar}"  /></font></td>
</tr>


<tr>
<td><label>I am from requestScopeVar :
</label></td>
<td><font color="RED"> <c:out value="${requestScopeVar}"/></font></td>
</tr>
<tr>
<td><label>I am from sessionScopeVar :
</label></td>
<td><font color="RED"> <c:out value="${sessionScopeVar}" /></font></td>
</tr>
<tr>
<td><label>I am from applicationScopeVar :
</label></td>
<td><font color="RED"> <c:out value="${applicationScopeVar}"/></font></td>
</tr>
</table>
</fieldset>
</body>
</html>



<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<br>
<form action="TestServlet" method="POST" id='test'>
<fieldset>
<legend>Plz enter login credentials : : </legend>
<table align="center">
<tr>
<td><label>Please Enter Your Name : </label></td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td><label>Please Enter Your City : </label></td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td><label>Please select your gender : </label></td>
<td><input type="radio" name="gender" value="Male" /> : Male <input
type="radio" name="gender" value="Female" /> : Female</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td align="right"><input type="submit" value="submit" /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>





#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.servlet.zip</groupId> <artifactId>JSPScopesProject</artifactId> <version>1.0</version> <packaging>war</packaging> <properties> <log4j-version>1.2.16</log4j-version> <java-version>1.8</java-version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> </project>


 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>com.cv.jsp.scopes.TestServlet</servlet-class> <init-param> <param-name>jspInitParam</param-name> <param-value>jspInitParamValue</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>start.html</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment