📄 formservlet.java
字号:
package com.learnweblogic.examples.ch2;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FormServlet extends HttpServlet{
/* The doGet method handles the initial invocation of
the servlet. The default service() method recognizes
that it has received a GET and calls this method
appropriately. It responds with a form that uses
the POST method to submit data.
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
res.setHeader("Pragma", "no-cache");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<body bgcolor=#FFFFFF>");
out.println("<h1>");
out.println("My Form!");
out.println("</h1>");
out.println("<font face=Helvetica>");
out.println("<form method=post action=FormServlet>");
out.println("<table border=0 bgcolor=#eeeeee cellspacing=10>");
out.println("<tr>");
out.println("<td>Username:</td>");
out.println("<td>");
out.println("<input type=TEXT name=username>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Your age:</td>");
out.println("<td>");
out.println("<input type=TEXT name=age>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("<p>");
out.println("<center>");
out.println("<input type=SUBMIT name=submit value=Submit>");
out.println("</center>");
out.println("</form>");
out.println("</font>");
out.println("</body>");
out.println("</html>");
}
/* Finally, include a separate doPost() method to be called
when the user responds by clicking on the submit button: */
/*
Responds to the "POST" query from the
original form supplied by the doGet() method.
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
// Set the content type of the response.
res.setContentType("text/html");
res.setHeader("Pragma", "no-cache");
PrintWriter pw = res.getWriter();
pw.println("<HTML><HEAD><TITLE>Form Completed</TITLE></HEAD>");
pw.println("<BODY>The information you have" +
" submitted is as follows:");
pw.println("<P>");
// Loop through all the name/value pairs.
Enumeration ParamNames = req.getParameterNames();
// Loop through all the name/value pairs.
while(ParamNames.hasMoreElements()) {
// Get the next name.
String ParamString = (String)ParamNames.nextElement();
// Print out the current name's value:
pw.println("<b>" + ParamString + ":</b> " +
req.getParameterValues(ParamString)[0]);
pw.println("<P>");
}
pw.println("</BODY></HTML>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -