Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)
–创建并提交表单
–使用Servlet处理表单
• 读取单个请求参数
• 读取多个表单
• 读取所有参数名称
–实例
• 注册会员
• 表单是web程序和用户交互的主要途径之一,例如:搜索引擎、用户登录、注册等操作都离不开表单的使用
• 常用表单元素
–input
» text
» password
» radio
» checkbox
» hidden
» file
» button
» reset
» submit
–select
» option
–textarea
–一个简单的表单(例如:登录),如下所示
–当单击“登录”按钮时表单被提交
–读取单个请求参数
• String user = request.getParameter(“user”);
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegisterServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("username:"+username);
out.println("username:"+password);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Servlet_Form/servlet/RegisterServlet" method="post">
用户名称:<input type="text" name="username"><br>
用户密码:<input type="password" name="password"><br>
<input type="submit" value="注册">
</body>
</html>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.michael.servletform.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/servlet/RegisterServlet</url-pattern>
</servlet-mapping>
</web-app>
测试
–读取多个表单
• String[] hobby = request.getParameterValues(“hobby”);
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Servlet_Form/servlet/RegisterServlet" method="post">
用户名称:<input type="text" name="username"><br>
用户密码:<input type="password" name="password"><br>
用户爱好:<input type="checkbox" name="hobby" value="1">游泳
<input type="checkbox" name="hobby" value="2">足球<br>
<input type="submit" value="注册">
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegisterServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] hobby = request.getParameterValues("hobby");
if(hobby!=null&&hobby.length>0){
for(int i=0;i<hobby.length;i++){
System.out.println(hobby[i]);
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("username:"+username);
out.println("password:"+password);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)相关推荐
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之14.Servlet请求头信息
–典型的请求头信息 –读取HTTP请求头 –使用表格显示所有请求头信息 –理解各种请求头的含义 –区分不同的浏览器类型 ##############Michael分割线################ ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2)
–提交表单的方法 • get • post –Servlet 生命周期 –使用Servlet 输出HTML页面 –获得Servlet初始化参数 –页面导航 • 请求重定向 –response.send ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之10.Web_工程结构
–简介 –Web应用程序的思想 –Web应用程序的目的 –Web工程结构 –web.xml 文件 –实例 • 创建一个简单的web应用程序 • 部署到tomcat中来运行 ############## ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之06.JDBC PreparedStatement
–PreparedStatement –为占位符"?"赋值 –使用PreparedStatement动态执行SQL语句 ####################Michael分割线 ...
- JDBC+Servlet+JSP整合开发之25.JSP动作元素
–jsp:useBean –jsp:setProperty –jsp:getProperty –jsp:forward –jsp:include –jsp:param –实例 ?计算器 ------- ...
- JDBC+Servlet+JSP整合开发之22.JSP简介
–对JSP的需求 –JSP的结构 –JSP的好处 –JSP实例 ?创建一个简单的JSP页面 ########################################### ? JSP –JSP ...
- JDBC+Servlet+JSP整合开发之26.JSP内建对象
–使用内建对象的目的 –内建对象 –out 内建对象 –request 内建对象 –response 对象 –session 内建对象 –pageContext 内建对象 –applic ...
- JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC
–Servlet 的优势与弊端 –JSP 的优势与弊端 –MVC 设计模式 –实例 ?使用MVC实现学生信息的添加.显示 -----------------------------START----- ...
- JDBC+Servlet+JSP整合开发之29-JSP表达式语言(EL)
–EL 简介 –EL的应用场合 –EL 的基本语法 –EL中的算术运算符 –EL中的关系运算符 –EL中的逻辑运算符 ------------------------------START- ...
最新文章
- 蓝桥杯国赛知识点汇总
- C#的委托事件在winform窗体中实现传值备忘
- linux shell 数组遍历方式(非原创)
- 欢迎使用markdown编辑器20181206
- linux grep sed awk
- oracle测试没响应,Oracle JDBC 没响应,是不是BUG?
- jcmd:JDK14中的调试神器
- Linux shell multifile content replace with sed
- 附录:更多集合操作命令
- 使用Lucene索引和检索POI数据
- Reids 批量删除有相同前缀的keys
- 在Linux中如何使用gdb调试C程序
- 使用 ASM 实现 Java 语言的“多重继承”
- 清明上河图轴卷图滑动
- vue项目接入高拍仪
- Veil-Evasion免杀
- android-759b1c是什么,[原创]hooker + jadx 动静结合内存漫游窥视某社交软件视频号功能的数据...
- 农村信用社答题小程序
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity 二维线段树||二维树状数组
- CS106B Assignment #4:Boggle
热门文章
- SQLmap工具常用命令
- Wireshark安装失败或找不到网络接口问题
- XamarinAndroid组件教程RecylerView动画组件使用动画(2)
- 单选框_vue实现单选框自定义样式
- https open api_钉钉API发送消息
- mfc程序转化为qt_工控编程,Qt 学习之路
- centos6.5下载卸载mysql_Linux CentOS 6.5 卸载、tar安装MySQL
- uni上传图片跨域_uni-app的项目实践心得
- 重磅!Nature子刊:利用GAN来​“深度伪造大脑数据”可以改善残疾人的脑机接口...
- android开发常见的设计模式,Android开发有哪些常用设计模式?