使用Servlet来进行数据类型的转化操作

建立一个登陆表单对象并对表单对象当中的age属性和birthday属性分别转化为int,Date类型.

登陆页面:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<!--SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 该语句规定输入的日期的格式必须为2017-01-27类型的否则将无法
执行会进行解析异常的抛出-->
	<h2 style="color: red;">请输入您的注册信息</h2>
	<form action="123">
	用户名:<input type="text" name="username"><br>
	密 码:<input type="password" name="password"><br>
	年 龄:<input type="text" name="age"><br>
	生 日:<input type="text" name="birthday"><br>
	<input type="submit" value="注册">
	</form>
</body>
</html>
servlet当中的配置信息.web.xml

<servlet>
  	<servlet-name>registerServlet</servlet-name>
  	<servlet-class>com.servlet.RegisterServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>registerServlet</servlet-name>
  	<url-pattern>/123</url-pattern>
  </servlet-mapping>

bean对象:

import java.util.Date;

public class UserBean {
	private String name;
	private String password;
	private int age;
	private Date birthday;
	public UserBean()
	{
		
	}
	public UserBean(String name,String password,int age,Date birthday)
	{
		this.name=name;
		this.password=password;
		this.age=age;
		this.birthday=birthday;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}
	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}


处理表单数据的Servlet类

import java.io.IOException;
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 com.bean.UserBean;

public class RegisterServlet  extends HttpServlet{

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// service方法将会接收来自于服务器端的方法
		System.out.println("进入Servlet类当中的service方法");
		request.setCharacterEncoding("UTF-8");
		String name=request.getParameter("username");
		String password=request.getParameter("password");
		String strAge=request.getParameter("age");
		String strBirthday=request.getParameter("birthday");
		int age=Integer.parseInt(strAge);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date birthday=null;
		try {
		//将字符串生日通过类型转换对象来转变为Date类型的日期	
			birthday=(Date) sdf.parse(strBirthday);
			System.out.println("转化后的生日日期为:"+birthday);
		} catch (ParseException e) {
			//当出现异常信息时
			System.out.println("日期转化失败");
			e.printStackTrace();
		}
		UserBean user=new UserBean(name, password, age,birthday);
		request.getSession().setAttribute("user",user);
		request.getRequestDispatcher("show.jsp").forward(request, response);
	}
	
}
进行数据显示的页面:show.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>进行学生的信息显示</title>
<%
	UserBean user=(UserBean)session.getAttribute("user");
%>
</head>
<body>
	<h1 style="color: red;">进行学生信息的显示</h1>
	<br>
	用户名:<%=user.getName() %>
	密码:<%=user.getPassword() %>
	年龄:<%=user.getAge() %>
	生日:<%=user.getBirthday() %>
</body>
</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值