使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
我在上一篇文章中介紹了 使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器,這篇將介紹使用Post方式提交數(shù)據(jù)到服務(wù)器,由于Post的方式和Get方式創(chuàng)建Web工程是一模一樣的,只用幾個(gè)地方的代碼不同所以,我就直接介紹不同的地方,第一個(gè)不同點(diǎn)是,提交方式不同,所以修改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 { } //使用Post方式向服務(wù)器提交數(shù)據(jù) public void doPost(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")); } } }
第二個(gè)需要修改的地方是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="post"> 用戶名:<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)目成功的部署到了瀏覽器上
在用戶名中輸入小志,在密碼中輸入123,當(dāng)瀏覽器中顯示登錄成功時(shí)表示登錄成功,因?yàn)槲以诜?wù)器中設(shè)置的正確的用戶名是小志,正確的密碼是123
當(dāng)在用戶名或者密碼中有一項(xiàng)輸入錯(cuò)誤時(shí)會(huì)顯示登錄失敗
關(guān)于使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法就給大家介紹這么多,希望對(duì)大家有所幫助!
- tomcat何時(shí)寫回響應(yīng)數(shù)據(jù)報(bào)的詳析
- Nginx + Tomcat實(shí)現(xiàn)請(qǐng)求動(dòng)態(tài)數(shù)據(jù)和請(qǐng)求靜態(tài)資源的分離詳解
- 解決Linux下Tomcat向MySQL插入數(shù)據(jù)中文亂碼問題
- 基于Tomcat 數(shù)據(jù)源的原理、配置、使用介紹
- Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫)
- Tomcat 7-dbcp配置數(shù)據(jù)庫連接池詳解
- 使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
- 在Tomcat服務(wù)器下使用連接池連接Oracle數(shù)據(jù)庫
- Tomcatc3p0配置jnid數(shù)據(jù)源2種實(shí)現(xiàn)方法解析
相關(guān)文章
Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè)
這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07SpringCloud?GateWay網(wǎng)關(guān)示例代碼詳解
這篇文章主要介紹了SpringCloud?GateWay網(wǎng)關(guān),Spring?cloud?Gateway的功能很多很強(qiáng)大,文中提到了Spring?Cloud?Gateway中幾個(gè)重要的概念,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-04-04Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解
這篇文章主要介紹了Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringCloud?eureka(server)微服務(wù)集群搭建過程
這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項(xiàng)目搭建的主要步驟和配置就是創(chuàng)建項(xiàng)目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07詳解SpringBoot上傳圖片到阿里云的OSS對(duì)象存儲(chǔ)中
這篇文章主要介紹了SpringBoot上傳圖片到阿里云的OSS對(duì)象存儲(chǔ)中,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10