ITKeyword,专注技术干货聚合推荐

注册 | 登录

Java Web学习(30): Servlet学习(三)

erlian1992 分享于 2016-08-01

推荐:Java Web学习(30): 使用JSP+Servlet+JavaBean实现用户登录

       实现用户登录        用户名admin,密码admin,登录成功使用服务器内部跳转到login_success2.jsp页面,并且提示登录成功的用 户名,如果登录失败则跳转到

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

        Servlet获取JSP九大内置对象

        Servlet与JSP九大内置对象对应关系:

        

        Servlet与表单

        Servlet如何获取表单数据呢?

        我们就不再一一讲解,具体的可以参考:http://www.runoob.com/servlet/servlet-form-data.html

        实例:获取表单数据

        实体类Users代码:

package com.entity;


import java.util.Date;


/**
 * 用户实体类
 * @author Administrator
 * @date 2016年7月25日
 */
public class Users {


 private String username; //用户名
 private String password; //密码
 private String email; //电子邮箱
 private String gender; //性别
 private Date birthday; //出生日期
 private String[] favorites; //爱好
 private String introduce; //自我介绍
 private boolean flag; //是否接受协议
 
 public Users() {


 }


 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 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[] getFavorites() {
 return favorites;
 }


 public void setFavorites(String[] favorites) {
 this.favorites = favorites;
 }


 public String getIntroduce() {
 return introduce;
 }


 public void setIntroduce(String introduce) {
 this.introduce = introduce;
 }


 public boolean getFlag() {
 return flag;
 }


 public void setFlag(boolean flag) {
 this.flag = flag;
 }
 
}

        Servlet层的RegServlet代码:  

package com.servlet;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.entity.Users;


/**
 * Servlet implementation class RegServlet
 */
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


 /**
  * @see Servlet#init(ServletConfig)
  */
 public void init(ServletConfig config) throws ServletException {
 // TODO Auto-generated method stub
 }


 /**
  * @see Servlet#destroy()
  */
 public void destroy() {
 // TODO Auto-generated method stub
 }


 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 doPost(request,response);
 }


 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 //防止中文乱码
        request.setCharacterEncoding("UTF-8");
 
 Users user = new Users();
 String username;
 String password;
 String gender;
 String email;
 String introduce;
 String flag;//是否接受协议
 Date birthday;
 String[] favorites;
 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 try{
 username = request.getParameter("username");
 password = request.getParameter("password");
 gender = request.getParameter("gender");
 email = request.getParameter("email");
 introduce = request.getParameter("introduce");
 //需要注意类型转化
 birthday = sdf.parse(request.getParameter("birthday"));
 //这个也比较特殊,需要注意
 if(request.getParameterValues("flag")!=null){
 flag = request.getParameter("flag");
 }else{
 flag = "false";
 }
 //用来获取多个复选按钮的值
 favorites = request.getParameterValues("favorite");
 
 user.setUsername(username);
 user.setPassword(password);
 user.setGender(gender);
 user.setEmail(email);
 user.setFavorites(favorites);
 user.setIntroduce(introduce);
 //特殊
 if(flag.equals("true")){
 user.setFlag(true);
 }else{
 user.setFlag(false);
 }
 user.setBirthday(birthday);
 
 //把注册成功的用户对象保存在session对象中
 request.getSession().setAttribute("regUser", user);
 //跳转到注册成功页面
 request.getRequestDispatcher("userinfo.jsp").forward(request,response);
 
 }catch(Exception e){
 e.printStackTrace();
 }
 }


}

推荐:java web 学习总结之 Servlet/JSP 编码问题

Servlet和JSP编码问题   字节流:  1.得到OutputStream  字节流   OutputStream os = response.getOutputStream();   用默认编码输出数据,就是本地编码GBK   os.

        注册页面reg.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>
<style type="text/css">
.label {
 width: 20%
}


.controler {
 width: 80%
}
</style>  
<script type="text/javascript" src="js/Calendar3.js"></script>
</head>
<body>
 <h1>用户注册</h1>
 <hr>
 <form name="regForm" action="RegServlet" method="post">
 <table border="0" width="800" cellspacing="0" cellpadding="0">
 <tr>
 <td class="lalel">用户名:</td>
 <td class="controler">
     <input type="text" name="username" />
 </td>
 </tr>
 <tr>
 <td class="label">密码:</td>
 <td class="controler">
      <input type="password" name="password" />
 </td>
 </tr>
 <tr>
 <td class="label">确认密码:</td>
 <td class="controler">
       <input type="password" name="confirmpass" />
 </td>
 </tr>
 <tr>
 <td class="label">电子邮箱:</td>
 <td class="controler">
     <input type="text" name="email" />
 </td>
 </tr>
 <tr>
 <td class="label">性别:</td>
 <td class="controler">
     <input type="radio" name="gender" checked="checked" value="Male" />男
     <input type="radio" name="gender" value="Female" />女
 </td>
 </tr>


 <tr>
 <td class="label">出生日期:</td>
 <td class="controler">
       <input name="birthday" type="text" id="control_date" size="10" maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" />
 </td>
 </tr>
 <tr>
 <td class="label">爱好:</td>
 <td class="controler">
      <input type="checkbox" name="favorite" value="nba" /> NBA &nbsp; 
      <input type="checkbox" name="favorite" value="music" /> 音乐 &nbsp; 
      <input type="checkbox" name="favorite" value="movie" /> 电影 &nbsp; 
      <input type="checkbox" name="favorite" value="internet" /> 上网 &nbsp;
 </td>
 </tr>
 <tr>
 <td class="label">自我介绍:</td>
 <td class="controler">
      <textarea name="introduce" rows="10" cols="40"></textarea>
 </td>
 </tr>
 <tr>
 <td class="label">接受协议:</td>
 <td class="controler">
      <input type="checkbox" name="flag" value="true" />是否接受霸王条款
 </td>
 </tr>
 <tr>
 <td colspan="2" align="center">
      <input type="submit" value="注册" />&nbsp;&nbsp;
  <input type="reset" value="取消" />&nbsp;&nbsp;
 </td>
 </tr>
 </table>
 </form>
</body>
</html>

        用户信息界面userinfo.jsp页面代码:

<%@page import="java.text.SimpleDateFormat"%>
<%@ 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>
<style type="text/css">
.title {
 width: 30%;
 background-color: #CCC;
 font-weight: bold;
}


.content {
 width: 70%;
 background-color: #CBCFE5;
}
</style> 
</head>
<body>
 <h1>用户信息</h1>
 <hr>
 <center>
 <jsp:useBean id="regUser" class="com.entity.Users" scope="session" />
 
 <table width="600" cellpadding="0" cellspacing="0" border="1">
 <tr>
 <td class="title">用户名:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="username" />
 </td>
 </tr>
 <tr>
 <td class="title">密码:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="password" />
 </td>
 </tr>
 <tr>
 <td class="title">性别:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="gender" />
 </td>
 </tr>
 <tr>
 <td class="title">E-mail:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="email" />
 </td>
 </tr>
 <tr>
 <td class="title">出生日期:</td>
 <td class="content">&nbsp; 
         <% 
                               SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                               String date = sdf.format(regUser.getBirthday());
                          %> 
                          <%=date%>
 </td>
 </tr>
 <tr>
 <td class="title">爱好:</td>
 <td class="content">&nbsp; 
 <% 
                       String[] favorites = regUser.getFavorites();
                       for(String fav:favorites){
                 %> 
                 <%=fav%> 
                 &nbsp;&nbsp; 
                 <% 
                        }
                  %>
 </td>
 </tr>
 <tr>
 <td class="title">自我介绍:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="introduce" /></td>
 </tr>
 <tr>
 <td class="title">是否介绍协议:</td>
 <td class="content">&nbsp;<jsp:getProperty name="regUser" property="flag" /></td>
 </tr>
 </table>
 </center>
</body>
</html>

        运行结果:

        用户注册页面:

        

        用户信息界面:


推荐:Java Web 之 Servlet 学习总结

Java Web 之 Servlet 学习总结 Servlet 简介 Servlet 是运行在服务端的java 小程序,他能够响应客户端的请求,并向客户端发送相应的响应结果。 整个请求的流程:

        Servlet获取JSP九大内置对象         Servlet与JSP九大内置对象对应关系:                  Servlet与表单         Servlet如何获取表单数据呢?         我们就不再一一讲解,具体的

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。