經過錄入用戶信息實例—體驗JavaBean與Servlet在JSP中應用的兩種不一樣的實現過程

在java web的學習過程裏面,javabean和servlet都是比較重要的兩部份內容。在開發中javabean能夠用來實現一些業務邏輯或者封裝一些業務的對象;而Servlet不但能夠用來處理HTTP請求中的業務邏輯,還能夠輸出HTML代碼來顯示指定頁面。下面就經過錄入用戶信息實例展開詳細的分析,以便更方便體驗JavaBean與Servlet在JSP頁面中應用的兩種不一樣的實現過程html




JavaBeanjava

在JSP頁面中應用JavaBean主要經過JSP動做標籤<jsp:useBean>、<jsp:getProperty>、<jsp:setProperty>來實現對JavaBean對象的操做。web

先來了解一下學習JavaBean中有幾個比較重要的知識點:正則表達式

setXXX()方法//提供爲JavaBean中的屬性賦值的方法app

getXXX()方法//提供公共的訪問方法
jsp

<jsp:useBean>:實例化javaBean對象ide

<jsp:getProperty>:獲取JavaBean的屬性信息。post

<jsp:setProperty>:對JavaBean的屬性在JSP頁面中賦值學習


本實例中實現檔案管理系統,在其中錄入用戶信息功能。其開發步驟以下:ui

(1)建立一個Person類,將其放置在com.lxy.bean包中,實現對用戶信息封裝。在person類中包含4個屬性.

package com.lxy.bean;
public class Person {
private String name;
private int age;
private String sex;
private String add;
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
public int getAge(){
    return age;
}
public void setAge(int age){
    this.age=age;
}
public String getSex(){
    return sex;
}
public void setSex(String sex){
    this.sex=sex;
}
public String getAdd(){
    return add;
}
public void setAdd(String add){
    this.add=add;
}
}


(2)建立程序的主頁面index.jsp,放置錄入用戶信息的表單。(提示:表單屬性名稱最好設置成爲JavaBean中的屬性名稱,這樣就能夠經過「<jsp:setProperty property="*">」的形式來接收全部參數,減小代碼量。若是將年齡文本框name屬性設置爲age,它對應Person類的age.)

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>添加用戶信息</title>
</head>
<body>
<form action="deal.jsp" method="post">
<table align="center"width="400"height="200"border="1">
<tr>
<td align="center"colspan="2"height="40">
<b>添加用戶信息</b>
</td>
</tr>
<tr>
<td align="right">姓 名:
</td>
<td><input type="text" name="name">
</td>
</tr>
<tr>
<td align="right">年  齡:
</td>
<td><input type="text"name="age">
</td>
</tr>
<tr>
<td align="right">性  別:
</td>
<td><input type="text" name="sex">
</td>
</tr>
<tr>
<td align="right">住 址:
</td>
<td><input type="text" name="add">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="添加">
</td>
</tr>
</table>
</form>
</body>
</html>


(3)建立deal.jsp的處理頁,用於對index.jsp頁面中表單的提交請求處理。該頁面將獲取表單所提交的全部信息,而後將獲取的用戶信息輸出到頁面中。

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>處理頁</title>
</head>
<body>
<%request.setCharacterEncoding("GB18030"); %>  //經過request的setCharacterEncoding()方法指定編碼格式進行解決亂碼問題
<jsp:useBean id="person" class="com.lxy.bean.Person" scope="page">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
<table align="center" width="400">
<tr>
<td align="right">姓 名:
</td>
<td><jsp:getProperty property="name" name="person"/>
</td>
</tr>
<tr>
<td align="right">年  齡:
</td>
<td><jsp:getProperty property="age" name="person"/>
</td>
</tr>
<tr>
<td align="right">性  別:
</td>
<td><jsp:getProperty property="sex" name="person"/>
</td>
</tr>
<tr>
<td align="right">住 址:
</td>
<td><jsp:getProperty property="add" name="person"/>
</td>
</tr>
</table>
</body>
</html>

最後運行頁面:


wKioL1LVPpLx4JNLAAEjlCMskAw630.jpg

顯示

wKiom1LVPqejO8nFAADcB5hZfvo730.jpg




Servlet


Servlet與JavaBean的方法有比較大的區別。

實現用戶信息的添加功能,並建立字符編碼過濾器,避免中文亂碼現象的產生。

(1)建立字符編碼過濾器對象,其名稱爲CharactorFilter類。該類實現了javax.servlet.Filter接口,並在doFilter()方法中對請求中的字符編碼格式進行設置:

package com.lixiyu;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharactorFilter implements Filter{
String encoding=null;                                 //字符編碼
public void destroy(){
encoding=null;
}
public void doFilter(ServletRequest request,ServletResponse response
        ,FilterChain chain)throws IOException,ServletException{
    if(encoding!=null){                                    //判斷字符編碼是否爲空
        request.setCharacterEncoding(encoding);            //設置request的編碼格式
        response.setContentType("text/html;charset="+encoding);//設置response字符編碼格式
    }
    chain.doFilter(request, response);//傳遞到下一過濾器
}
public void init(FilterConfig filterConfig)throws ServletException{
    encoding=filterConfig.getInitParameter("encoding");  //獲取初始化參數
}
}


(2)建立了過濾器對象以後,須要對過濾器進行必定的配置才能夠正常使用:

<filter>
   <filter-name>CharactorFilter</filter-name>
   <filter-class>com.lixiyu.CharactorFilter</filter-class>
   <init-param>
     <param-name>encoding</param-name>
     <param-value>UTF-8</param-value>
   </init-param>
 </filter>
 <filter-mapping>
   <filter-name>CharactorFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

技巧:在web.xml文件中配置過濾器,其過濾器的URL映射能夠使用正則表達式進行配置,若是上面中使用的「/*」來匹配全部請求。


(3)建立AddServlet類,繼承HttpServlet,是處理添加用戶信息請求的Servlet對象:

package com.lixiyu;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class AddServlet
 */
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
                                                                                                                                             
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {  //處理GET請求
        // 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 {  //處理POST請求
        // TODO Auto-generated method stub
        PrintWriter out=response.getWriter();//獲取PrintWriter
        String name=request.getParameter("name");//獲取用戶信息
        String age=request.getParameter("age");
        String sex=request.getParameter("sex");
        String add=request.getParameter("add");
        out.print("<h2>用戶信息添加成功</h2><hr>");//輸出用戶信息
        out.print("姓名:"+name+"<br>");
        out.print("年齡:"+age+"<br>");
        out.print("性別:"+sex+"<br>");
        out.print("地址:"+add+"<br>");
        out.flush();//刷新流
        out.close();//關閉流
    }
}

AddServlet類主要經過doPost()方法實現添加圖書管理信息請求的處理,其方式是將所獲取的信息數據直接輸出到頁面中。


(4)在web.xml中對Servlet進行配置:

<servlet>
  <servlet-name>AddServlet</servlet-name>
  <servlet-class>com.lixiyu.AddServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>AddServlet</servlet-name>
  <url-pattern>/AddServlet</url-pattern>
  </servlet-mapping>


(5)建立index.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>
    <form action="AddServlet" method="post">
        <table align="center" border="1" width="350">
            <tr>
                <td class="2" align="center" colspan="2">
                    <h2>添加用戶信息</h2>
                </td>
            </tr>
            <tr>
                <td align="right">姓    名:</td>
                <td>
                    <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td align="right">年    齡:</td>
                <td>
                    <input type="text" name="age">
                </td>
            </tr>
            <tr>
                <td align="right">性    別:</td>
                <td>
                    <input type="text" name="sex">
                </td>
            </tr>
            <tr>
                <td align="right">地    址:</td>
                <td>
                    <input type="text" name="add">
                </td>
            </tr>
            <tr>
                <td class="2" align="center" colspan="2">
                    <input type="submit" value="添 加">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>


最後運行:

wKiom1LWXAiyBRK6AAEqMVbOuB8889.jpg

顯示

wKioL1LWW_yhJzB_AAEBZ9e47fk370.jpg

相關文章
相關標籤/搜索