📄 subscribeservlet.java
字号:
package personalPortal;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* Title: Professional Java Servlet Programming - Chapter 2
* Description: Servlet to manage subscriptions to the newsletter(s).
* This will present the user with a form if they have not
* filled in the form, or process the request if the form
* has been completed.
* Copyright: Copyright (c) 2001
* Company:
* @author Andrew Harbourne-Thomas
* @version 1.0
*/
public class SubscribeServlet extends GenericServlet implements DataMapping {
/** Constant included as hidden value in the Subscription
* form so we know if this is a new request
*/
public static final String SUB_FORM = "SubForm";
/** used in subscription page as email parameter */
public static final String EMAIL = "email";
/** Daily subscription */
public static final String DAILY = "Daily";
/** Weekly subscription */
public static final String WEEKLY = "Weekly";
/** Monthly subscription */
public static final String MONTHLY = "Monthly";
/**
* This processes requests to this servlet, determining if they
* are new requests (to be given a form) or form submissions
* for processing subscription requests
*
* @param request The object containing the client request
* @param response The object used to send the response back
*/
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String subscribeForm = request.getParameter(SUB_FORM);
if (subscribeForm != null) {
//its a subscribe request
out.println(processSubscription(request));
}
else {
//its a form request
out.println(printForm());
}
}
/**
* This method processes subscription requests. In this case we
* perform little validation, but in a real world applciaiton
* we would thoroughly validate the data, and then save it to a
* database.
*
* @param request - the ServletRequest object (to extract parameters)
*/
private StringBuffer processSubscription(ServletRequest request) {
StringBuffer sub = new StringBuffer();
String email = request.getParameter(EMAIL);
String daily = request.getParameter(DAILY);
String weekly = request.getParameter(WEEKLY);
String monthly = request.getParameter(MONTHLY);
//basic validation of email
if (!isEmailOK(email)) {
//failed validation
sub.append("<h3>Error Processing Request</h3>");
sub.append("Email entered is invalid. Please go ");
sub.append("<a href=\"javascript:history.go(-1)\">Back</a>");
sub.append(" to try again.");
}
else {
//here, if the request was successful, we would normally
//save the data to a database
sub.append("<h3>Request Processed</h3>");
sub.append("<p>Your request is being processed. ");
sub.append("You should receive an email confirmation shortly. ");
sub.append("<p>Thank you.");
}
return sub;
}
/**
* Performs Basic validation of the email address.
*
* @return Indicates if the email is OK (true), or failed validation (false)
*/
private boolean isEmailOK(String emailToValidate) {
if (emailToValidate == null ||
emailToValidate.trim().length() == 0) {
return false;
}
//check for an @
int at = emailToValidate.indexOf("@");
if (at == -1) {
return false;
}
//check for a dot after the @
int dot = emailToValidate.indexOf(".", at);
if (dot == -1) {
return false;
}
return true;
}
/**
* This returns the form for the client to fill in.
*
* @return The HTML form
*/
private StringBuffer printForm() {
StringBuffer form = new StringBuffer();
form.append("<form action=\"").append(PORTAL_RESOURCE);
form.append("\" method=\"post\">");
form.append("<input type=\"hidden\" name=\"");
form.append(PORTAL_VIEW_PARAM_NAME).append("\" value=\"");
form.append(PORTAL_VIEW_PARAM);
form.append("\"><input type=\"hidden\" name=\"");
form.append(PAGE_REQUESTED_NAME).append("\" value=\"");
form.append(SUBSCRIBE_NAME);
form.append("\"><input type=\"hidden\" name=\"");
form.append(SUB_FORM).append("\" value=\"");
form.append(SUB_FORM);
form.append("\"><table align=\"center\" width=400>");
form.append("<tr><td colspan=2><h3 align=\"center\">");
form.append("Subscribe to the newsletters</h3></td></tr>");
form.append("<tr><td width=\"50%\">Email Address</td>");
form.append("<td width=\"50%\"><input type=\"text\" name=\"");
form.append(EMAIL).append("\">");
form.append("</td></tr><tr><td>Daily Newsletter</td>");
form.append("<td><input type=\"checkbox\" name=\"");
form.append(DAILY).append("\"></td></tr>");
form.append("<tr><td>Weekly Newsletter</td>");
form.append("<td><input type=\"checkbox\" name=\"");
form.append(WEEKLY).append("\"></td></tr>");
form.append("<tr><td>Monthly Newsletter</td>");
form.append("<td><input type=\"checkbox\" name=\"");
form.append(MONTHLY).append("\"></td></tr>");
form.append("<tr><td colspan=2 align=center>");
form.append("<input type=\"submit\" value=\"Submit Form\">");
form.append("<input type=\"reset\" value=\"Clear Form\"></td></tr>");
form.append("</table></form>");
return form;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -