- Project name: Simple Beer Project using JSP and SERVLET.
- Web server: Apache Tomcat 6.0.
To run this project u need these files:
1)web.xml
2)LForm.jsp
3)BeerSelect.java
4)LCost.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.jexamples.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>LForm.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ 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="yellow"> yellow </option>
<option value="blue"> blue </option>
<option value="brown"> brown </option>
<option value="red"> red </option>
</select>
<br>
<input type="submit" name="submitc" value="Submit Color" />
</form>"
</body>
</html>
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class BeerSelect extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ float cst=0;
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>access init parameters</h3>");
Enumeration e = getServletConfig().getInitParameterNames();
while(e.hasMoreElements())
{
out.println("<br> param name ="+e.nextElement()+"<br>");
}
out.println("<hr> <hr>main email is "+ getServletConfig().getInitParameter("mainEmail"));
out.println("<br>");
out.println("admin email is "+ getServletConfig().getInitParameter("adminEmail"));
String cl[]={"yellow","blue","brown","red"};
for(int i=0;i<4;i++)
{
if(c.equals(cl[i]))
{
cst=(i+1)*125;
}
}
//Setting Session attribute
HttpSession session = request.getSession();
session.setAttribute("color", c);
session.setAttribute("cost",String.valueOf(cst));
out.println("<hr> cost="+cst+"<hr>");
RequestDispatcher view = request.getRequestDispatcher("LCost.jsp");
view.include(request,response);
//view.forward(request,response); // try this too n see diff in include n forward
}
}
<%@ page language="java" import="java.util.*,javax.servlet.*,javax.servlet.http.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'WCost.jsp' starting page</title>
</head>
<%
HttpSession sess = request.getSession();
String colr = (String) sess.getAttribute("color");
String cost = (String) sess.getAttribute("cost");
%>
<body bgcolor="<%=colr%>">
<table bgcolor="<%=colr%>">
<tr>
<td>Brand Chosen is <%=colr%></td>
<td>Cost inclusive of all taxes <%=cost%></td>
</tr>
</table>
</body>
</html>
No comments:
Post a Comment