Java Tutorial/Servlet/Form
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Get Form Text Field as a Servlet parameter
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String name = req.getParameter("name");
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, " + name);
out.println("</BODY></HTML>");
}
public String getServletInfo() {
return "A servlet that knows the name of the person to whom it"s" +
"saying hello";
}
}
Servlet Based GuestBook
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet {
private Vector entries = new Vector();
private long lastModified = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
printHeader(out);
printForm(out);
printMessages(out);
printFooter(out);
}
// Add a new entry, then dispatch back to doGet()
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
handleForm(req, res);
doGet(req, res);
}
private void printHeader(PrintWriter out) throws ServletException {
out.println("<HTML><HEAD><TITLE>Guestbook</TITLE></HEAD>");
out.println("<BODY>");
}
private void printForm(PrintWriter out) throws ServletException {
out.println("<FORM METHOD=POST>"); // posts to itself
out.println("<B>Please submit your feedback:</B><BR>");
out.println("Your name: <INPUT TYPE=TEXT NAME=name><BR>");
out.println("Your email: <INPUT TYPE=TEXT NAME=email><BR>");
out.println("Comment: <INPUT TYPE=TEXT SIZE=50 NAME=comment><BR>");
out.println("<INPUT TYPE=SUBMIT VALUE=\"Send Feedback\"><BR>");
out.println("</FORM>");
out.println("<HR>");
}
private void printMessages(PrintWriter out) throws ServletException {
String name, email, comment;
Enumeration e = entries.elements();
while (e.hasMoreElements()) {
GuestbookEntry entry = (GuestbookEntry) e.nextElement();
name = entry.name;
if (name == null) name = "Unknown user";
email = entry.email;
if (name == null) email = "Unknown email";
comment = entry.rument;
if (comment == null) comment = "No comment";
out.println("<DL>");
out.println("<DT><B>" + name + "</B> (" + email + ") says");
out.println("<DD><PRE>" + comment + "</PRE>");
out.println("</DL>");
// Sleep for half a second to simulate a slow data source
try { Thread.sleep(500); } catch (InterruptedException ignored) { }
}
}
private void printFooter(PrintWriter out) throws ServletException {
out.println("</BODY>");
}
private void handleForm(HttpServletRequest req,
HttpServletResponse res) {
GuestbookEntry entry = new GuestbookEntry();
entry.name = req.getParameter("name");
entry.email = req.getParameter("email");
entry.rument = req.getParameter("comment");
entries.addElement(entry);
// Make note we have a new last modified time
lastModified = System.currentTimeMillis();
}
public long getLastModified(HttpServletRequest req) {
return lastModified;
}
}
class GuestbookEntry {
public String name;
public String email;
public String comment;
}
Servlet Form Processor CheckBox
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.IOException;
public class MyServlet extends HttpServlet
{
public void doPost (HttpServletRequest req, HttpServletResponse res) throws IOException
{
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
out.println("<html><head><title>Basic Form Processor Output</title></head>");
out.println("<body>");
out.println("<h1>Here is your Form Data</h1>");
//extract the form data here
String title = req.getParameter("title");
String name = req.getParameter("name");
String city = req.getParameter("city");
String country = req.getParameter("country");
String tel = req.getParameter("tel");
String age = req.getParameter("age");
// extracting data from the checkbox field
String[] interests = req.getParameterValues("interests");
//output the data into a web page
out.println("Your title is " + title);
out.println("<br>Your name is " + name);
out.println("<br>Your city is " + city);
out.println("<br>Your country is " + country);
out.println("<br>Your tel is " + tel);
out.println("<br>Your interests include<ul> ");
for (int i=0;i<interests.length; i++) {
out.println("<li>" + interests[i]);
}
out.println("</ul>");
out.println("<br>Your age is " + age);
out.println("</body></html>");
}
}
Servlet Post Form Data to Self Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
System.out.println("command: "+request.getParameter("command"));
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Help Page</title></head><body>");
out.println("<h2>Please submit your information</h2>");
out.println("<form method=\"post\" action =\"" + request.getContextPath() +"/MyServlet\" >");
out.println("<table border=\"0\"><tr><td valign=\"top\">");
out.println("Your first name: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"firstname\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("Your last name: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"lastname\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("Your email: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"email\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
out.println("</table></form>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,java.io.IOException{
Enumeration paramNames = request.getParameterNames();
String parName;
boolean emptyEnum = false;
if (! paramNames.hasMoreElements())
emptyEnum = true;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Submitted Parameters</title></head><body>");
if (emptyEnum){
out.println("<h2>Sorry, the request does not contain any parameters</h2>");
} else {
out.println("<h2>Here are the submitted parameter values</h2>");
}
while(paramNames.hasMoreElements()){
parName = (String) paramNames.nextElement();
out.println("<strong>" + parName + "</strong> : " + request.getParameter(parName));
out.println("<br />");
}
out.println("</body></html>");
}
}