最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法

 更新時(shí)間:2016年04月21日 10:41:46   作者:梅沙小子  
這篇文章將介紹向服務(wù)器發(fā)送數(shù)據(jù),并且服務(wù)器將數(shù)據(jù)的處理結(jié)果返回給客戶端,本文給大家介紹使用Get方式向服務(wù)器發(fā)送數(shù)據(jù),感興趣的朋友一起學(xué)習(xí)吧

這篇文章將介紹向服務(wù)器發(fā)送數(shù)據(jù),并且服務(wù)器將數(shù)據(jù)的處理結(jié)果返回給客戶端,這次先介紹使用Get方式向服務(wù)器發(fā)送數(shù)據(jù),下篇將介紹使用Post方式向服務(wù)器發(fā)送數(shù)據(jù),需要的朋友參考下吧!

實(shí)現(xiàn)方式分為以下幾步:

第一步:使用MyEclipse創(chuàng)建一個(gè)Web project,項(xiàng)目命名為WebProject->在src文件夾中新建一個(gè)包名為com.fyt.org的包
->在包中新建一個(gè)Servlet,Servlet命名為L(zhǎng)oginServlet,并在LoginServlet.Java中添加下面的代碼

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 { 
//獲取從瀏覽器中發(fā)送過來的用戶名 
String username = request.getParameter("username"); 
//獲取從客戶端發(fā)送過來的密碼 
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)中打印用戶名和密碼 
System.out.println("username=" + username); 
System.out.println("password=" + password); 
//獲得一個(gè)輸出流 
OutputStream os = response.getOutputStream(); 
//如果用戶名和密碼都輸入正確 
if("小志".equals(username) && "123".equals(password)) { 
//將字符發(fā)送至瀏覽器中 
os.write("登錄成功".getBytes("utf-8")); 
} 
else { 
//將字符串發(fā)送到瀏覽器中 
os.write("登錄失敗".getBytes("utf-8")); 
} 
} 
//使用Post方式向服務(wù)器提交數(shù)據(jù) 
public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
} 
}

第二步:啟動(dòng)Tomcat服務(wù)器,Tomcat服務(wù)器的啟動(dòng)方式可以參考我的博客在MyEclipse上部署Tomcat服務(wù)器

第三步:修改WebProject項(xiàng)目中的WebRoot目錄下的index.jsp中的代碼,index.jsp中的代碼如下

<%@ 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="get"> 
用戶名:<input type="text" name="username"><br> 
密碼:<input type="password" name="password"><br> 
<input type="submit" value="提交"> 
</form> 
</body> 
</html> 

第四步:將項(xiàng)目部署到Tomcat服務(wù)器上,部署方式如下

1、點(diǎn)擊下圖中圈出的圖標(biāo)


2、Project中選擇WebProject,并且單擊Add按鈕


3、Server中選擇Tomcat 7.x,并且單擊finish按鈕


4、單擊OK按鈕,此時(shí)WebProject項(xiàng)目已經(jīng)成功的部署到了Tomcat服務(wù)器上


第五步:打開瀏覽器,在瀏覽器中輸入http://192.168.1.102:8080/WebProject/index.jsp,在瀏覽器中顯示了下圖所示的界面表示成功的訪問到了服務(wù)器中的數(shù)據(jù)


在用戶名中輸入小志,在密碼中輸入123,單擊登錄按鈕后,彈出了登錄成功界面后表示登錄成功了,因?yàn)樵O(shè)置的正確的用戶名是小志,正確的密碼是123


當(dāng)在用戶名和密碼中輸入錯(cuò)誤的密碼后,會(huì)提示登錄失敗

關(guān)于使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法,小編就給大家介紹這么多,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論

山东| 山阴县| 宜章县| 马山县| 钟祥市| 安乡县| 尉犁县| 宜良县| 柏乡县| 乐至县| 彰武县| 任丘市| 富顺县| 中西区| 五台县| 尚义县| 全州县| 新安县| 嘉祥县| 平度市| 漳州市| 青海省| 会昌县| 衡阳县| 东丰县| 大兴区| 洱源县| 台南市| 六枝特区| 通州区| 汕头市| 新巴尔虎左旗| 陇西县| 合江县| 含山县| 开远市| 江都市| 新蔡县| 塔城市| 栾城县| 游戏|