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

来源:互联网 发布:手游抢激活码软件 编辑:程序博客网 时间:2024/06/07 07:15

建立一个登陆表单对象并对表单对象当中的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) */@Overrideprotected 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>






阅读全文
0 0