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
	}

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值