使用Post方式提交数据到Tomcat服务器

我在上一篇博客中介绍了 使用Get方式提交数据到Tomcat服务器,这篇博客中将介绍使用Post方式提交数据到服务器,由于Post的方式和Get方式创建Web工程是一模一样的,只用几个地方的代码不同所以,我就直接介绍不同的地方,第一个不同点是,提交方式不同,所以修改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方式向服务器提交数据
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}

	//使用Post方式向服务器提交数据
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//获取从浏览器中发送过来的用户名
		String username = request.getParameter("username");
		
		//获取从客户端发送过来的密码
		String password = request.getParameter("password");
		
		//使用iso8859-1编码将username转换成字节数组
		//再使用utf-8把字节数组转换成字符串
		username = new String(username.getBytes("iso8859-1"), "utf-8");

		//在控制台中打印用户名和密码
		System.out.println("username=" + username);
		System.out.println("password=" + password);
		
		//获得一个输出流
		OutputStream os = response.getOutputStream();
		
		//如果用户名和密码都输入正确
		if("小志".equals(username) && "123".equals(password)) {
			
			//将字符发送至浏览器中
			os.write("登录成功".getBytes("utf-8"));
		}
		else {
			//将字符串发送到浏览器中
			os.write("登录失败".getBytes("utf-8"));
		}
	}
}


第二个需要修改的地方是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>

修改完成后将项目部署到Tomcat服务器上,部署方式可以参考我的博客 使用Get方式提交数据到Tomcat服务器

部署完成后在浏览器中输入http://192.168.1.102:8080/WebProject/index.jsp,当浏览器中显示下图所示的界面表示项目成功的部署到了浏览器上



在用户名中输入小志,在密码中输入123,当浏览器中显示登录成功时表示登录成功,因为我在服务器中设置的正确的用户名是小志,正确的密码是123



当在用户名或者密码中有一项输入错误时会显示登录失败


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值