Info.java
资源名称:WAPpush.zip [点击查看]
上传用户:weisa_1
上传日期:2007-10-14
资源大小:287k
文件大小:10k
源码类别:
手机WAP编程
开发平台:
Java
- package info;
- import commercials.Commercials;
- import members.Members;
- import feedback.Feedback;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.text.SimpleDateFormat;
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- /**
- * Info pages
- *
- */
- public final class Info extends HttpServlet {
- private static final String XML_VERSION = "<?xml version="1.0"?>";
- private static final String CONTENT_TYPE = "text/vnd.wap.wml";
- private static final String SI_CONTENT_TYPE = "text/vnd.wap.si";
- private static final String DOC_TYPE = "<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"n" +
- " "http://www.wapforum.org/DTD/wml_1.1.xml">";
- /**
- * Called by the server to allow a servlet to handle a GET request.
- *
- * @param request a <code>HttpServletRequest</code> value
- * @param response a <code>HttpServletResponse</code> value
- * @exception ServletException if an error occurs
- * @exception IOException if an error occurs
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- /**
- * Called by the server to allow a servlet to handle a POST request.
- *
- * @param request a <code>HttpServletRequest</code> value
- * @param response a <code>HttpServletResponse</code> value
- * @exception ServletException if an error occurs
- * @exception IOException if an error occurs
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /* Session tracking. See the description in index.jsp */
- HttpSession session = request.getSession();
- boolean cookiesCheck = request.getHeader("cookie") != null && request.isRequestedSessionIdValid();
- final String jsessionID = cookiesCheck ? "" : ";jsessionid=" + session.getId();
- String dob = null; String name = null; String sex = null; String email = null; String pushMessages = null;
- /* get user information from the current session object */
- try{
- name = (String)session.getAttribute("name");
- dob = (String)session.getAttribute("dob");
- sex = (String)session.getAttribute("sex");
- email = (String)session.getAttribute("email");
- pushMessages = (String)session.getAttribute("pushservices");
- }
- catch (Exception E){
- E.printStackTrace();
- }
- /* check if WTAI functionality is supported by the client user agent */
- final boolean wtaiSupport = wtaiSupported(request);
- /* Administrator's user name */
- final String ADMIN_USER = "admin";
- /* start generating the response ... */
- response.setContentType(CONTENT_TYPE);
- /* prevent caching of the response */
- response.setHeader("cache-control", "no-cache");
- PrintWriter out = response.getWriter();
- out.println(XML_VERSION);
- out.println(DOC_TYPE);
- out.println("<wml>");
- out.println("<!-- provides a way back using the prev element -->n" +
- "<template>" +
- " <do type="prev">" +
- " <prev/>" +
- " </do>" +
- "</template>");
- out.println("<!-- Information page about news, opening hours, prices etc -->n" +
- "<card title="Info" id="main">");
- boolean showAdminMenu = name != null && name.equals(ADMIN_USER);
- if (showAdminMenu) {
- out.println("<!-- only for System Administrator an "AdminMenu" is shown -->n" +
- "<do type="accept" label="AdminMenu" name="1">n" +
- " <go href="" + request.getRequestURI() + jsessionID + "#admin"/>n" +
- "</do>n" +
- "<p><anchor title="Admin info">Admin info" +
- " <go href="" + request.getRequestURI() + jsessionID + "#admin"/>n" +
- "</anchor></p>");
- }
- String commercial = Commercials.getRandomCommercial();
- out.println(" <do type="accept" label="New" name="new">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#new"/>" +
- " </do>" +
- " <do type="accept" label="Opening hours" name="open">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#open"/>" +
- " </do>" +
- " <do type="accept" label="Prices" name="prices">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#prices"/>" +
- " </do>" +
- " <do type="accept" label="Contact" name="contact">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#contact"/>" +
- " </do>" +
- " <p>" +
- (!commercial.equals("") ? "*** " + commercial + "***<br/>" : "") +
- " <anchor title="New">New" +
- " <go href="" + request.getRequestURI() + jsessionID + "#new"/>" +
- " </anchor><br/>" +
- " <anchor title="Opening hours">Opening hours" +
- " <go href="" + request.getRequestURI() + jsessionID + "#open"/>" +
- " </anchor><br/>" +
- " <anchor title="Prices">Prices" +
- " <go href="" + request.getRequestURI() + jsessionID + "#prices"/>" +
- " </anchor><br/>" +
- " <anchor title="Contact">Contact" +
- " <go href="" + request.getRequestURI() + jsessionID + "#contact"/>" +
- " </anchor>" +
- " </p>" +
- "</card>");
- out.println("<!-- A card displaying opening hours of the Mobile Zoo -->" +
- "<card title="Opening hours" id="open">" +
- " <p><strong>Mon-Fri:</strong> 10am-8pm<br/>" +
- " <strong>Sat-Sun:</strong> 12am-9pm</p>" +
- "</card>");
- out.println("<!-- Contact information -->" +
- "<card title="Contact information" id="contact">" +
- " <p>Zoo Hill 7, 33101 Zootown<br/>");
- if(wtaiSupport) {
- out.println("<anchor title="Make call">tel. 555-555-555" +
- " <go href="wtai://wp/mc;555555555"/>" +
- "</anchor><br/>");
- } else {
- out.println("tel. 555-555-555<br/>");
- }
- out.println(" <anchor title="Feedback">Feedback" +
- " <go href="feedback" + jsessionID+ ""/>" +
- " </anchor></p>" +
- "</card>");
- out.println("<card title="New" id="new">" +
- " <p><strong>Today:</strong> Check out our new "Safari" family restaurant next to the gate! Exotic cuisine in exotic setting!<br/>" +
- " <strong>Last week:</strong>   Flamingo pond is re-opened after the renovation." +
- " </p>" +
- "</card>");
- out.println("<card title="Prices" id="prices">" +
- "<p>Adults: 5 €<br/>" +
- "Children: 3 €<br/>" +
- "Pensioners: 3 €<br/>" +
- "Season pass: 20 €<br/>");
- if (dob != null) {
- out.println("Member information:<br/>");
- try {
- int age = ageCalculator(dob);
- if(age < 16) {
- out.println("<em>Today free entry for children!</em>");
- }
- else if(age < 65) {
- out.println("Special offer for you  <strong>" + name + "</strong>!  <em>As a member you will get" +
- " 2 € reduction out of the normal Adults ticket price.</em>");
- }
- else {
- out.println("<em>Today free entry for all senior members of the Zoo!</em>");
- }
- }
- catch (Exception E) {
- // E.printStackTrace();
- }
- }
- out.println(" </p>" +
- "</card>");
- if (showAdminMenu) {
- out.println("<card title="Administrator's info" id="admin">" +
- " <p><anchor title="Feedback">Feedback" +
- " <go href="" + request.getRequestURI() + jsessionID + "#votestats"/>" +
- " </anchor><br/>" +
- " <anchor title="Users">Users" +
- " <go href="" + request.getRequestURI() + jsessionID + "#userstats"/>" +
- " </anchor>" +
- " <do type="accept" label="Votes" name="1">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#votestats"/>" +
- " </do>" +
- " <do type="accept" label="Users" name="2">" +
- " <go href="" + request.getRequestURI() + jsessionID + "#userstats"/>" +
- " </do>" +
- " </p>" +
- "</card>");
- out.println("<card title="Vote statistics" id="votestats"><p>");
- out.println(Feedback.viewVotes());
- out.println("</p></card>");
- out.println("<card title="User statistics" id="userstats">" +
- "<p>" + viewUsers() +
- "</p></card>");
- }
- out.println("</wml>");
- }
- /**
- * <code>viewUsers</code> views the number of users (males and females).
- *
- * @return a <code>String</code> value
- */
- private static String viewUsers() {
- int numberOfUsers = 0;
- double numberOfMales = 0;
- Statement stmt = Members.getStatement();
- try {
- ResultSet rs = stmt.executeQuery("SELECT sex FROM members");
- while (rs.next()) {
- ++numberOfUsers;
- if (rs.getString("sex").equals("m")) ++numberOfMales;
- }
- }
- catch (SQLException E){
- E.printStackTrace();
- }
- if(numberOfUsers == 0)
- return "No users";
- else
- return "Users: " + numberOfUsers + "<br/>Male: " + Math.round(numberOfMales / (double)numberOfUsers * 100) + "%"
- + "<br/>Female: " + Math.round(((double)numberOfUsers - numberOfMales) / (double)numberOfUsers * 100) + "%";
- }
- /**
- * Calculates the current age on the basis of date of birth (dob) given in format "yyyy.mm.dd"
- *
- * @param dob a <code>String</code> value
- * @return an <code>int</code> value
- */
- public static int ageCalculator(String dob) {
- SimpleDateFormat s = new SimpleDateFormat("yyyy.MM.dd");
- try {
- java.util.Date d = s.parse(dob);
- Calendar c = Calendar.getInstance();
- c.setTime(d);
- return Calendar.getInstance().get(Calendar.YEAR) - c.get(Calendar.YEAR);
- }
- catch(Exception e) { System.err.println(e); }
- return -1;
- }
- /**
- * Check if the client phone supports wtai functionality.
- * The user-agent header information is utilised.
- */
- private boolean wtaiSupported(HttpServletRequest request) {
- String wtaiSupportFile = getInitParameter("wtaisupport");
- String userAgent = request.getHeader("User-Agent");
- if(userAgent == null) return false;
- userAgent = userAgent.toLowerCase();
- wtaiSupportFile = getServletContext().getRealPath(wtaiSupportFile);
- try {
- BufferedReader reader = new BufferedReader(new FileReader(wtaiSupportFile));
- String line;
- while((line = reader.readLine()) != null) {
- line = line.trim().toLowerCase();
- if(line.equals("")) break;
- if(userAgent.startsWith(line)) return true;
- }
- }
- catch (java.io.IOException e) {
- System.err.println(e);
- }
- return false;
- }
- }