Java/Servlets/Form
Содержание
Basic Form processor
<source lang="java">
/* <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.ru/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.ru/xml/ns/j2ee http://java.sun.ru/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>BasicFormProcessor</servlet-name> <servlet-class>BasicFormProcessor</servlet-class> </servlet> <servlet-mapping> <servlet-name>BasicFormProcessor</servlet-name> <url-pattern>/formProcessor</url-pattern> </servlet-mapping>
</web-app>
- /
import java.io.IOException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BasicFormProcessor 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("
Here is your Form Data
");//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("out.println("
Your name is " + name); out.println("
Your city is " + city); out.println("
Your country is " + country); out.println("
Your tel is " + tel);
Your interests include
- ");
for (int i = 0; i < interests.length; i++) {
out.println("
- " + interests[i]); } out.println("
out.println("
Your age is " + age); out.println("</body></html>"); }
}
</source>
feedback Form
<source lang="java">
<HTML><HEAD><TITLE> feedback Form </TITLE></HEAD>
<BODY>Feedback Form
<FORM NAME="ParameterPost" ACTION="/servlets-examples/PrintFormParams" METHOD="POST">
Name: | <INPUT TYPE="TEXT" SIZE="25" NAME="Person"> |
Email: | <INPUT TYPE="TEXT" SIZE="25" MAXLENGTH="40" NAME="emailaddress"> |
How did you find this site? |
<SELECT NAME="from" SIZE="1"> <OPTION VALUE = "Website" SELECTED>Another Website</OPTION> <OPTION VALUE = "search engine">A search engine</OPTION> <OPTION VALUE = "friend">A friend told you</OPTION> <OPTION VALUE = "email">From an email</OPTION> <OPTION VALUE = "unlisted">Another way not listed here</OPTION> </SELECT> |
How would you rate my website: |
<INPUT TYPE="radio" NAME = "rating" VALUE = "Excellent"> Excellent <INPUT TYPE="radio" NAME = "rating" VALUE = "Good"> Good <INPUT TYPE="radio" NAME = "rating" VALUE = "Average" CHECKED> Average <INPUT TYPE="radio" NAME = "rating" VALUE = "Poor"> Poor <INPUT TYPE="radio" NAME = "rating" VALUE = "Overhaul"> Needs an Overhaul |
Comments or Suggestions: |
<TEXTAREA ROWS="6" COLS="40" WRAP="PHYSICAL" Name="suggestions"> Enter any comments or suggestions you have here.</TEXTAREA> |
Do you think this form looks nice? |
<INPUT TYPE="CHECKBOX" NAME="formrating" VALUE="yes"> |
<INPUT TYPE="SUBMIT" VALUE="Send Comments" ALIGN="MIDDLE"> <INPUT TYPE="RESET" VALUE="Clear Form" ALIGN="MIDDLE"> |
Input Name | Value | ");
---|---|
" + inputName + " | "); out.println("" + value + " |
" + inputName + " | "); out.println("Null |
");
out.println(htmlFooter); }
}
</source>
HTML Forms with Servlets
React to user form input
<source lang="java">
import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { //set the MIME type of the response to "text/html" response.setContentType("text/html"); //use a PrintWriter send text data to the client java.io.PrintWriter out = response.getWriter(); //output the HTML content out.println("<html><head>"); out.println("<title>Help Page</title></head><body>");out.println("
Please submit your information
");//make sure method="post" so that the servlet service method //calls doPost in the response to this form submit out.println("<form method=\"post\" action =\"" + request.getContextPath() + "/FirstServlet\" >");out.println("
"); out.println("Your first name: | ");
out.println("<input type=\"text\" name=\"firstname\" size=\"20\">");out.println(" |
"); out.println("Your last name: | ");
out.println("<input type=\"text\" name=\"lastname\" size=\"20\">");out.println(" |
"); out.println("Your email: | ");
out.println("<input type=\"text\" name=\"email\" size=\"20\">");out.println(" |
"); out.println("<input type=\"submit\" value=\"Submit Info\"> |
out.println("</body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { //display the parameter names and values Enumeration paramNames = request.getParameterNames(); String parName; boolean emptyEnum = false; if (!paramNames.hasMoreElements()) emptyEnum = true; //set the MIME type of the response to "text/html" response.setContentType("text/html"); //use a PrintWriter send text data to the client java.io.PrintWriter out = response.getWriter(); //Begin assembling the HTML content out.println("<html><head>"); out.println("<title>Submitted Parameters</title></head><body>"); if (emptyEnum) {out.println("
No parameters
");} else {out.println("
Here are the submitted parameter values
");} while (paramNames.hasMoreElements()) { parName = (String) paramNames.nextElement(); out.println("" + parName + " : " + request.getParameter(parName)); out.println("
"); } out.println("</body></html>"); }
}
</source>