1. 程式人生 > >servlet獲取表單資料

servlet獲取表單資料

1.sevlet獲取表單資料

在很多的情況下,我們需要在瀏覽器,Web伺服器和後臺程式之間傳遞資料。瀏覽器使用兩種方法可將這些資訊傳遞到Web伺服器,分別為Get方法和Post方法。

2.Get方法,Post方法

get方法:GET方法是預設的從瀏覽器向Web伺服器傳遞資訊的方法,它會產生一個很長的字串,出現在瀏覽器的位址列中。如果您要向伺服器傳遞的是密碼或其他的敏感資訊,請不要使用GET方法.GET方法有大小限制:請求字串中最多隻能有1024個字元。

頁面和已編碼的資訊中間用?字元分隔,如下所示:

http://www.news.com/news?newsid=5

 Post方法:另一個向後臺程式傳遞資訊的比較可靠的方法是POST方法。POST方法打包資訊的方式與GET方法基本相同,但是POST 方法不是把資訊作為URL中?字元後的文字字串進行傳送,而是把這些資訊作為一個單獨的訊息。訊息以標準輸出的形式傳到後臺程式,您可以解析和使用這些標準輸出。Servlet使用doPost()方法處理這種型別的請求。

3.servlet獲取資料的幾種方法

  • getParameter():您可以呼叫 request.getParameter() 方法來獲取表單引數的值。
  • getParameterValues():如果引數出現一次以上,則呼叫該方法,並返回多個值,例如複選框。
  • getParameterNames():如果您想要得到當前請求中的所有引數的完整列表,則呼叫該方法。

 下面我們用servlet 實現一個註冊的小功能,在login.jsp表單中獲取資料,最後在hello.jsp打印出來

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="loginServlet" method="post">
使用者名稱:<input type="text" name="username"/><br>
密碼:<input type="password" name="password"/><br>
性別:<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女"/>女<br>
愛好:<input type="checkbox" name="hobby" value="籃球"/>籃球
<input type="checkbox" name="hobby" value="羽毛球"/>羽毛球
<input type="checkbox" name="hobby" value="網球"/>網球<br>
所在城市:<select name="city">
        <option>----請選擇----</option>
        <option value="baijing">北京</option>
        <option value="shanghai">上海</option>
        <option value="hainan">海南</option>
       </select><br>
       <input type="submit" value="點選註冊"/>
</form>

</body>
</html>

LoginServlet.java

package com.demo.servlet;

import java.io.IOException;
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 javax.servlet.http.HttpSession;

import com.demo.JavaBean.User;


public class loginServlet extends HttpServlet {
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     request.setCharacterEncoding("UTF-8");
     //獲取username,password等值
     String username=request.getParameter("username");
     String password=request.getParameter("password");
     String sex=request.getParameter("sex");
     String[] hobbys=request.getParameterValues("hobby");
     String[] city=request.getParameterValues("city");
    // 建立User物件,把獲取的值新增到這個物件
     User user=new User();
     user.setUsername(username);
     user.setPassword(password);
     user.setSex(sex);
     user.setHobby(hobbys);
     //獲取session物件,把User物件存到session中
     HttpSession session=request.getSession();
     session.setAttribute("user", user);
     //重定向到hello.jsp頁面
     response.sendRedirect(request.getContextPath()+"/hello.jsp");
	}

}

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="200" cellpadding="0" cellspacing="0">
   <tr>
   <th>使用者</th>
   	<th>${sessionScope.user.username}</th>
   </tr>
   <tr>
   <th>性別</th>
   	<th>${sessionScope.user.sex}</th>
   </tr>
   <tr>
   <th>愛好</th>
   </tr>
   <tr>
      	<th>${sessionScope.user.hobby[0]}</th>
     	<th>${sessionScope.user.hobby[1]}</th>
   </tr>
   
</table>
</body>
</html>

User.java

package com.demo.JavaBean;

import javax.swing.Spring;

public class User {
	private String username;
	private String password;
	private String sex;
	private String[] hobby;
	private Spring city;
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String[] getHobby() {
		return hobby;
	}
	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}
	public Spring getCity() {
		return city;
	}
	public void setCity(Spring city) {
		this.city = city;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}

web.xml

配置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>servletdemo</display-name>
  <servlet>
  		<servlet-name>login</servlet-name>
  		<servlet-class>com.demo.servlet.loginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  		<servlet-name>login</servlet-name>
  		<url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
</web-app>