- Project name: Simple Beer Project using JSP and SERVLET.
- Web server: Apache Tomcat 6.0.
There is one WForm.jsp , BeerSelect.java (servlet program) and web.xml in coding part.
1)web.xml
2)WForm.jsp
3)BeerSelect.java
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.jspexamples.web.BeerSelect</servlet-class>
<init-param>
<param-name>adminEmail</param-name>
<param-value>likewecare@kingbeer.com</param-value>
</init-param>
<init-param>
<param-name>mainEmail</param-name>
<param-value>blooper@kingbeer.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>WForm.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)WForm.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Beer Choice</title>
</head>
<br>
<body>
<h1 align="center"> Beer Selection Page</h1>
<hr>
<form name="beerForm" method="post" action="SelectBeer.do">
<p> Select Beer Characteristics <br>
Color:
<select name="color" size="1">
<option value="light"> light </option>
<option value="amber"> amber </option>
<option value="brown"> brown </option>
<option value="dark"> dark </option>
</select>
<br>
<input type="submit" name="submitc" value="Submit Color" />
</form>"
</body>
</html>
3)BeerSelect.java
package com.jspexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BeerSelect extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
out.println("<br> Got Beer Color "+c);
out.println("<hr> <h3> 1st way to access init parameters</h3>");
Enumeration e = getServletConfig().getInitParameterNames();
while(e.hasMoreElements())
{
out.println("<br> param name ="+e.nextElement()+"<br>");
}
out.println("<hr> <h3> 2nd way to access init parameters</h3>");
Enumeration e2 = this.getServletConfig().getInitParameterNames();
while(e2.hasMoreElements())
{
out.println("<br> param name ="+e2.nextElement()+"<br>");
}
out.println("<hr> <h3> 3rd way to access init parameters</h3>");
Enumeration e3 = getInitParameterNames();
while(e3.hasMoreElements())
{
out.println("<br> param name ="+e3.nextElement()+"<br>");
}
out.println("<hr> <h3> 4th way to access init parameters</h3>");
ServletConfig scfg = getServletConfig();
Enumeration e4 = scfg.getInitParameterNames();
while(e4.hasMoreElements())
{
out.println("<br> param name ="+e4.nextElement()+"<br>");
}
out.println("<hr> <hr>main email is "+ getServletConfig().getInitParameter("mainEmail"));
out.println("<br>");
out.println("admin email is "+ getServletConfig().getInitParameter("adminEmail"));
}
}
No comments:
Post a Comment