我在上一篇文章中介紹了 使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器,這篇將介紹使用Post方式提交數(shù)據(jù)到服務(wù)器,由于Post的方式和Get方式創(chuàng)建Web工程是一模一樣的,只用幾個(gè)地方的代碼不同所以,我就直接介紹不同的地方,第一個(gè)不同點(diǎn)是,提交方式不同,所以修改LoginServlet.Java中的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package com.fyt.org; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public LoginServlet() { super (); } public void destroy() { super .destroy(); } //使用Get方式向服務(wù)器提交數(shù)據(jù) public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //使用Post方式向服務(wù)器提交數(shù)據(jù) public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取從瀏覽器中發(fā)送過(guò)來(lái)的用戶(hù)名 String username = request.getParameter( "username" ); //獲取從客戶(hù)端發(fā)送過(guò)來(lái)的密碼 String password = request.getParameter( "password" ); //使用iso8859-1編碼將username轉(zhuǎn)換成字節(jié)數(shù)組 //再使用utf-8把字節(jié)數(shù)組轉(zhuǎn)換成字符串 username = new String(username.getBytes( "iso8859-1" ), "utf-8" ); //在控制臺(tái)中打印用戶(hù)名和密碼 System.out.println( "username=" + username); System.out.println( "password=" + password); //獲得一個(gè)輸出流 OutputStream os = response.getOutputStream(); //如果用戶(hù)名和密碼都輸入正確 if ( "小志" .equals(username) && "123" .equals(password)) { //將字符發(fā)送至瀏覽器中 os.write( "登錄成功" .getBytes( "utf-8" )); } else { //將字符串發(fā)送到瀏覽器中 os.write( "登錄失敗" .getBytes( "utf-8" )); } } } |
第二個(gè)需要修改的地方是index.jsp,將index.jsp中的代碼修改成下面的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ page language= "java" import = "java.util.*" pageEncoding= "utf-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <base href= "<%=basePath%>" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv= "pragma" content= "no-cache" > <meta http-equiv= "cache-control" content= "no-cache" > <meta http-equiv= "expires" content= "0" > <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > <!-- <link rel= "stylesheet" type= "text/css" href= "styles.css" > --> </head> <body> <form action= "servlet/LoginServlet" method= "post" > 用戶(hù)名:<input type= "text" name= "username" ><br> 密碼:<input type= "password" name= "password" ><br> <input type= "submit" value= "提交" > </form> </body> </html> |
修改完成后將項(xiàng)目部署到Tomcat服務(wù)器上,部署方式可以參考我的博客使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器
部署完成后在瀏覽器中輸入http://192.168.1.102:8080/WebProject/index.jsp,當(dāng)瀏覽器中顯示下圖所示的界面表示項(xiàng)目成功的部署到了瀏覽器上
在用戶(hù)名中輸入小志,在密碼中輸入123,當(dāng)瀏覽器中顯示登錄成功時(shí)表示登錄成功,因?yàn)槲以诜?wù)器中設(shè)置的正確的用戶(hù)名是小志,正確的密碼是123
當(dāng)在用戶(hù)名或者密碼中有一項(xiàng)輸入錯(cuò)誤時(shí)會(huì)顯示登錄失敗
關(guān)于使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法就給大家介紹這么多,希望對(duì)大家有所幫助!