www.pudn.com > WAPpush.zip > Info.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 = "";
    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 = "";

    /**
     * Called by the server to allow a servlet to handle a GET request. 
     *
     * @param request a HttpServletRequest value
     * @param response a HttpServletResponse 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 HttpServletRequest value
     * @param response a HttpServletResponse 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("");

	out.println("\n" +
		    "");

	out.println("\n" +
		    "");
	boolean showAdminMenu = name != null && name.equals(ADMIN_USER);

	if (showAdminMenu) {
	    out.println("\n" +
			"\n" + 
			" \n" +
			"\n" +
			"

Admin info" + " \n" + "

"); } String commercial = Commercials.getRandomCommercial(); out.println(" " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "

" + (!commercial.equals("") ? "*** " + commercial + "***
" : "") + " New" + " " + "
" + " Opening hours" + " " + "
" + " Prices" + " " + "
" + " Contact" + " " + " " + "

" + "
"); out.println("" + "" + "

Mon-Fri: 10am-8pm
" + " Sat-Sun: 12am-9pm

" + "
"); out.println("" + "" + "

Zoo Hill 7, 33101 Zootown
"); if(wtaiSupport) { out.println("tel. 555-555-555" + " " + "
"); } else { out.println("tel. 555-555-555
"); } out.println(" Feedback" + " " + "

" + "
"); out.println("" + "

Today: Check out our new \"Safari\" family restaurant next to the gate! Exotic cuisine in exotic setting!
" + " Last week:   Flamingo pond is re-opened after the renovation." + "

" + "
"); out.println("" + "

Adults: 5 €
" + "Children: 3 €
" + "Pensioners: 3 €
" + "Season pass: 20 €
"); if (dob != null) { out.println("Member information:
"); try { int age = ageCalculator(dob); if(age < 16) { out.println("Today free entry for children!"); } else if(age < 65) { out.println("Special offer for you  " + name + "!  As a member you will get" + " 2 € reduction out of the normal Adults ticket price."); } else { out.println("Today free entry for all senior members of the Zoo!"); } } catch (Exception E) { // E.printStackTrace(); } } out.println("

" + "
"); if (showAdminMenu) { out.println("" + "

Feedback" + " " + "
" + " Users" + " " + " " + " " + " " + " " + " " + " " + " " + "

" + "
"); out.println("

"); out.println(Feedback.viewVotes()); out.println("

"); out.println("" + "

" + viewUsers() + "

"); } out.println("
"); } /** * viewUsers views the number of users (males and females). * * @return a String 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 + "
Male: " + Math.round(numberOfMales / (double)numberOfUsers * 100) + "%" + "
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 String value * @return an int 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; } }