Servlet中的get和post方法

get方法和post方法的比较

1.首先建一个Servlet类(没有post和get方法时,一个service方法就可以替代)

package com.alex.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	
	
	
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	//shift + alt + s -> override implement method
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("get...");
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("post...");
		super.doPost(req, resp);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决post乱码问题
		request.setCharacterEncoding("UTF-8");
		
		//跳转页面servlet->jsp
		response.setContentType("text/html;charset=UTF-8");
		
		String uname= request.getParameter("uname");
		String pwd= request.getParameter("pwd");
		System.out.println("uname->"+uname);
		System.out.println("pwd->"+pwd);
		
		if(uname.equals("alex")&&pwd.equals("123")){
			response.getWriter().println("登录成功");
		}else{
			response.getWriter().println("登录失败,用户名或密码不正确");
		}
	}

	@Override
	public void destroy() {
		
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		
		super.init();
	}

	
	


}

2.分别用get和post两种方法的Jsp页面向Servlet提交数据
1)get(method=“get”)Jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>web project 02</title>
</head>
<body>
		<form action="LoginServlet" method="get">
		uname:<input type="text" name="uname" /><br />
		pwd:<input type="password" name="pwd" /><br />
		<input type="submit" value="提交" />
		</form>
</body>
</html>

url 会显示参数(信息容易泄露,不安全);数据的传递是有限的不支持传递大量数据
在这里插入图片描述

2)post(method=“post”)Jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>web project 02</title>
</head>
<body>
		<form action="LoginServlet" method="post">
		uname:<input type="text" name="uname" /><br />
		pwd:<input type="password" name="pwd" /><br />
		<input type="submit" value="提交" />
		</form>
</body>
</html>

参数在url里面看不到了;支持传递大量数据
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值