Servlet 取表单的值
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>ServletInfoForm</display-name><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>RegServlet</servlet-name><servlet-class>servlet.RegServlet</servlet-class></servlet><servlet-mapping><servlet-name>RegServlet</servlet-name><url-pattern>/servlet/RegServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">.label{width:20%}.controler{width:80%}</style><script type="text/javascript" src="js/Calender3.js"></script></head><body><h1>用户注册</h1><hr/><form name="regForm" action="servlet/RegServlet" method="post"><table><tr><td class="label">用户名:</td><td class="controler"><input type="text" name="username"/></td></tr><tr><td>密码:</td><td><input type="password" name="password"/></td></tr><tr><td>确认密码:</td><td><input type="password" name="mypassword"/></td></tr><tr><td>电子邮箱:</td><td><input type="text" name="email"/></td></tr><tr><td>性别:</td><td>男<input type="radio" name="gender" checked="checked" value="Male"/> 女<input type="radio" name="gender" value="Female"/> </td></tr><tr><td>出生日期:</td><td><input name="birthday" type="text" id="control_date" size="10"maxlength="10" οnclick="new Calendar().show(this);" readonly="readonly" /></td></tr><tr><td>爱好:</td><td>打游戏<input type="checkbox" name="hobby" value="game"/> 跑步<input type="checkbox" name="hobby" value="run"/> 上网<input type="checkbox" name="hobby" value="internet"/> 足球<input type="checkbox" name="hobby" value="football"/> </td></tr><tr><td>自我介绍:</td><td><textarea name="introduce" rows="10" cols="40"></textarea></td></tr><tr><td>接受协议:</td><td><input type="checkbox" name="protocol"/>是否接受霸王条款</td></tr><tr><td colspan="2" align="center"><input type="submit" name="register" value="注册"/> <input type="reset" name="cancel" value="取消"/> </td> </tr> </table></form></body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head><body><h1>用户信息</h1><hr/><jsp:useBean id="userInfo" class="entity.Users" scope="session"></jsp:useBean>用户名:<jsp:getProperty property="username" name="userInfo"/><br>密码:<jsp:getProperty property="password" name="userInfo"/><br/>邮箱:<jsp:getProperty property="email" name="userInfo"/><br/>性别:<jsp:getProperty property="gender" name="userInfo"/><br/>生日:<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");String birthday = sdf.format(userInfo.getBirthday());%><%=birthday %><br/>爱好:<%String[] hobbys = userInfo.getHobby();for(int i=0; i < hobbys.length; i++){%><%=hobbys[i] %><% }%><br/>是否同意协议:<jsp:getProperty property="protocol" name="userInfo"/><br/> </body>
</html>
package entity;import java.util.Date;public class Users {public Users(){}private String username;private String password;private String mypassword;private String email;private String gender;private Date birthday;private String[] hobby;private String introduce;private boolean protocol;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getMypassword() {return mypassword;}public void setMypassword(String mypassword) {this.mypassword = mypassword;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String[] getHobby() {return hobby;}public void setHobby(String[] hobby) {this.hobby = hobby;}public String getIntroduce() {return introduce;}public void setIntroduce(String introduce) {this.introduce = introduce;}public boolean isProtocol() {return protocol;}public void setProtocol(boolean protocol) {this.protocol = protocol;}}
package servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import entity.Users;public class RegServlet extends HttpServlet {/*** Constructor of the object.*/public RegServlet() {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 {doPost(request, response);}/*** 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 {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String username, password, email, gender, introduce;Date birthday;String[] hobby;boolean protocol;Users user = new Users();try {username = request.getParameter("username");password = request.getParameter("password");email = request.getParameter("email");gender = request.getParameter("introduce");birthday = sdf.parse(request.getParameter("birthday"));hobby = request.getParameterValues("hobby");if(request.getParameter("protocol") != null){protocol = true;}else{protocol = false;}user.setUsername(username);user.setPassword(password);user.setEmail(email);user.setGender(gender);user.setBirthday(birthday);user.setHobby(hobby);user.setProtocol(protocol);request.getSession().setAttribute("userInfo", user); request.getRequestDispatcher("../userInfo.jsp").forward(request,response);} catch (ParseException e) {e.printStackTrace();} }/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}
Servlet 取表单的值相关推荐
- 取表单radio值时
取表单radio值时 在 js 中取值时,用 e.detail.value.xxx 或 e.detail.value["xxx"],其中 xxx 为 name 属性值. poste ...
- 计算机vfp返回值,VFP 中 怎 样 获 取 表 单 的 返 回 值
在 调 用 自 定 义 的 对 话 框 表 单 时, 我 们 往 往 需 要 获 得 该 表 单 执 行 后 的 选 择 结 果 或 是 某 些 控 件 的 值 等 等. 许 多 人 采 用 的 方 ...
- 使用 Servlet 读取表单数据
Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析: getParameter():可以调用 request.getParameter() 方法来获取表单参数的值. getP ...
- 观看慕课老师milanlover视频Tomcat装在Servlet的三种情况+Servlet获取表单数据+Servlet相对路径
在src新建TestServlet1: package servlet;import java.io.IOException; import java.io.PrintWriter;import ja ...
- vue 给form表单赋值_vue获取form表单的值示例
vue获取form表单的值示例 这里使用的是Element-ui组件 html: JS: var phone = this.phone; var password = this.password; v ...
- Jquery中获取表单的值并提交
今天写了一个ajax的交互,通过获取表单的值,自己组织post的方式提交. 但是却出现了提交的值为空,后来检查后发现,在提交表单的值得处理的时机有些问题. 一开始先是获取了值,然后在单击提交按钮后传值 ...
- rails设置表单默认值amp;amp;隐藏表单
需求: 用户没有登录时游客评论,需要游客输入用户名和评论内容:当用户已登录时,读取session获取用户名,隐藏输入用户名的表达,用户直接发表评论. 用户评论代码: <h2>Add a c ...
- htmlparser设置表单属性值
想用htmlparser实现自动填写表单并提交,但暂时实现不了提交,计划通过httpclient实现.htmlparser可实现设置表单属性值,但貌似意义不大,其主要价值在解析html网页.设置表单属 ...
- js入门·表单详解一(修改表单属性,修改表单元素值)
实在javascript入门·Document对象入门讲解(访问表单,创建新页,获取页标题) 一文中,我们已经把表单的一些基本访问等弄清楚了,下面我们深入的学下表单的属性以及对表单元素的简单操作! 演 ...
最新文章
- SpringBoot-Security 具体案例、 实现安全框架、权限控制、aop切入
- 压箱底总结:流系统端到端一致性对比
- 使用jMeter构造大量并发HTTP请求进行微服务性能测试
- android reboot 定时重启可靠吗,请教一下有人做过安卓系统 reboot 重启之后,appium 能自动重新连接吗...
- python request 留位置4
- 华为与美国公司就授权5G平台展开初期谈判;Linux 中存在严重漏洞;Microsoft 发布 Cosmos DB GA 版……...
- Leetcode 刷题笔记(三) —— 数组类型解题方法三:滑动窗口
- mysql 数据字典详解_MySQL8功能详解——数据字典
- MVP模式——Okhttp实现下载图片并带有进度 【Android Demo】
- 灭菌仓密封性测试方案
- OpenCV图像锐化
- Spring 事件发布机制@EventListener源码深度解析
- DS18B20+数码管显示
- php大文件读取excel分割,如何用phpspreadsheet来切割excel大文件(附代码)
- Apache OpenNLP介绍(一)
- 逐步实现一个简易的飞机大战(c++)
- mysql tcmalloc_Tcmalloc优化MySQL内存管理
- 视频大数据技术在智慧城市建设中的应用
- 微信中页面二次分享小图标丢失问题
- 【动网论坛7.1 sp1 修改】-去掉阿里巴巴广告