本文主要是介绍使用 GET 方法读取表单数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HelloForm源码:
package firstweb;// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;// 扩展 HttpServlet 类
public class HelloForm extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 设置响应内容类型response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();String title = "使用 GET 方法读取表单数据";String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "+ "transitional//en\">\n";out.println(docType + "<html>\n" + "<head><title>" + title+ "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"+ "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n"+ " <li><b>名字</b>:" + request.getParameter("first_name")+ "\n" + " <li><b>姓氏</b>:" + request.getParameter("last_name")+ "\n" + "</ul>\n" + "</body></html>");}
}
web.xml源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>helloServlet</servlet-name><servlet-class>firstweb.HelloServlet</servlet-class></servlet><servlet><servlet-name>HelloWorld</servlet-name><servlet-class>firstweb.HelloWorld</servlet-class></servlet><servlet><servlet-name>HelloForm</servlet-name><servlet-class>firstweb.HelloForm</servlet-class></servlet><servlet-mapping><servlet-name>HelloForm</servlet-name><url-pattern>/HelloForm</url-pattern></servlet-mapping><servlet-mapping><servlet-name>helloServlet</servlet-name><url-pattern>/sayHi</url-pattern></servlet-mapping><servlet-mapping><servlet-name>HelloWorld</servlet-name><url-pattern>/HelloWorld</url-pattern></servlet-mapping>
</web-app>
静态页面:
<html>
<body><form action="HelloForm" method="GET">名字:<input type="text" name="first_name"> <br /> 姓氏:<inputtype="text" name="last_name" /> <input type="submit" value="提交" /></form>
</body>
</html>
通过浏览器输入测试:
http://localhost:8080/Servlet/hello.htm
效果:
这篇关于使用 GET 方法读取表单数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!