home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2001 March
/
PCWorld_2001-03_cd.bin
/
Software
/
Komercni
/
VAgeJava
/
ivj35
/
setup
/
JSP.Cab
/
F27261_LeapYear.java
< prev
next >
Wrap
Text File
|
2000-08-10
|
6KB
|
140 lines
package com.ibm.ivj.wte.samples.leapyear;
// Licensed Material - Property of IBM
// (C) Copyright IBM Corp. 2000 - All Rights Reserved
//
// DISCLAIMER:
// The following [enclosed] code is sample code created by IBM
// Corporation. This sample code is not part of any standard IBM product
// and is provided to you solely for the purpose of assisting you in the
// development of your applications. The code is provided 'AS IS',
// without warranty or condition of any kind. IBM shall not be liable for any damages
// arising out of your use of the sample code, even if IBM has been
// advised of the possibility of such damages.
/**
* Insert the type's description here.
* Creation date: (05/22/2000 11:14:52 AM)
* @author: Administrator
*/
public class LeapYear extends javax.servlet.http.HttpServlet {
private static final String copyright =
"(c) Copyright IBM Corporation 2000.";
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Returns the requested parameter
*
* @param request Object that encapsulates the request to the servlet
* @param parameterName The name of the parameter value to return
* @param checkRequestParameters when true, the request parameters are searched
* @param checkInitParameters when true, the servlet init parameters are searched
* @param isParameterRequired when true, an exception is thrown when the parameter cannot be found
* @param defaultValue The default value to return when the parameter is not found
* @return The parameter value
* @exception java.lang.Exception Thrown when the parameter is not found
*/
public String getParameter(javax.servlet.http.HttpServletRequest request, String parameterName, boolean checkRequestParameters, boolean checkInitParameters, boolean isParameterRequired, String defaultValue) throws Exception {
String[] parameterValues = null;
String paramValue = null;
// Get the parameter from the request object if necessary.
if (checkRequestParameters)
{
parameterValues = request.getParameterValues(parameterName);
if (parameterValues != null)
paramValue = parameterValues[0];
}
// Get the parameter from the servlet init parameters if
// it was not in the request parameter.
if ( (checkInitParameters) && (paramValue == null) )
paramValue = getServletConfig().getInitParameter(parameterName);
// Throw an exception if the parameter was not found and it was required.
// The exception will be caught by error processing and can be
// displayed in the error page.
if ( (isParameterRequired) && (paramValue == null) )
throw new Exception("Parameter " + parameterName + " was not specified.");
// Set the return to the default value if the parameter was not found
if (paramValue == null)
paramValue = defaultValue;
return paramValue;
}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
javax.servlet.ServletContext sc;
javax.servlet.RequestDispatcher rd;
try
{
com.ibm.ivj.wte.samples.leapyear.LeapYearBean leapYearBean = null;
// instantiate the bean.
leapYearBean = (com.ibm.ivj.wte.samples.leapyear.LeapYearBean) java.beans.Beans.instantiate(getClass().getClassLoader(),"com.ibm.ivj.wte.samples.leapyear.LeapYearBean");
request.setAttribute("leapYearBean", leapYearBean);
// Initialize the bean startYear property
if (!getParameter(request, "startYear", true, true, true, null).equals("")) {
leapYearBean.setStartYear(Integer.valueOf(getParameter(request, "startYear", true, true, true, null)).intValue());
}
// find next 10 leap years
leapYearBean.findLeapYears();
sc = getServletContext();
rd = sc.getRequestDispatcher("/JSP/Sample3/LeapYearResults.jsp"); // for JSP 1.0
// rd = sc.getRequestDispatcher("/JSP/Sample3/LeapYearResults091.jsp"); // for JSP 0.91
rd.forward(request, response);
}
catch(Throwable theException)
{
// uncomment the following line when unexpected exceptions
// are occuring to aid in debugging the problem.
//theException.printStackTrace();
response.setContentType("text/html");
java.io.PrintWriter out =response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Find Next 10 Leap Years</TITLE></HEAD>");
out.println("<BODY BGCOLOR=#C0C0C0>");
out.println("<H2>Exception Occurred</H2>");
out.println(theException);
out.println("<P><I>Note: If the exception is java.lang.NumberFormatException, your input is probably not a number.<I>");
out.println("</BODY></HTML>");
}
}
}