JSTL Conditional Example

Click here to download eclipse supported ZIP file




 

    
package com.cv.jsp.jstl;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 JSTlConditionalServlet
 
 @author Chandra Vardhan
 */
public class JSTlConditionalServlet extends HttpServlet {
  
  private static final Logger LOGGER = Logger.getLogger(JSTlConditionalServlet.class);

  /**
   @see HttpServlet#HttpServlet()
   */
  public JSTlConditionalServlet() {
    super();
  }

  /**
   @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doGet(HttpServletRequest ,HttpServletResponse ) of JSTlConditionalServlet class... ");
    response.setContentType("text/html");
    String name = request.getParameter("name");  
    LOGGER.info("Param name 'name' value is : " + name);
    request.setAttribute("userType", name);
    List<String> commentList = new ArrayList<String>();
    commentList.add(name);
    commentList.add("This site rocks....");
    commentList.add("This site coollll...");
    commentList.add("This site stupid....");
    LOGGER.info("LOGGER : request attribute (commentList) value set...");
    request.setAttribute("commentList",commentList);
    LOGGER.info("LOGGER : Page redirecting to conditionalJSTL.jsp...");
    RequestDispatcher dispatcher = request
        .getRequestDispatcher("conditionalJSTL.jsp");
    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 JSTlConditionalServlet class... ");
    
    doGet(request, response);
  }

}





<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
${param.input}
</body>
</html>



<%@ page language="java" import="java.util.*"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<br>
<fieldset>
<legend>Welcome to the page...</legend>
<br>
<table align="center">
<c:forEach var="comment" items="${commentList}">
<c:if test="${comment eq 'chandra'}">
<tr>
<td><font color="red">${comment} your website is
cool...</font></td>
</tr>
</c:if>
<c:if test="${comment ne 'chandra'}">
<tr>
<td>${comment}</td>
</tr>
</c:if>
</c:forEach>
</table>
<br>
<c:if test="${userType eq 'member'}">
<jsp:include page="includeComments.jsp"></jsp:include>
</c:if>
<c:if test="${userType eq 'ceo'}">
I m CEO I have all access...
</c:if>
<c:if test="${userType eq 'admin'}">
I am admin I too have all the access...
</c:if>
<table align="center">
<c:choose>
<c:when test="${userType eq 'member'}">
<jsp:include page="includeComments.jsp"></jsp:include>
</c:when>
<c:when test="${userType eq 'ceo'}">
I m CEO I have all access...
</c:when>
<c:when test="${userType eq 'admin'}">
I am admin I too have all the access...
</c:when>
<c:otherwise>
Your Content is best!!!
</c:otherwise>
</c:choose>
</table>
</fieldset>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
<form action="commentProcess.jsp">
<textarea rows="10" cols="30" name="input" ></textarea><br>
<input name="comment" type="submit" value="Add comment">
</form>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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><br>
<form action="JSTlConditionalServlet" method="get" id='test'>
<fieldset>
<legend>Wow... Sorry... Plz enter login credentials again...</legend>
<table align="center">
<tr>
<td><label>Enter Name : </label><input type="text"
name="name" /></td>
</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>JSTLConditionalProject</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"> <display-name>JSTl Conditional Servlet</display-name> <context-param> <param-name>email</param-name> <param-value>cvardhan.k@gmail.com</param-value> </context-param> <servlet> <servlet-name>JSTlConditionalServlet</servlet-name> <servlet-class>com.cv.jsp.jstl.JSTlConditionalServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JSTlConditionalServlet</servlet-name> <url-pattern>/JSTlConditionalServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>start.jsp</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment