初试form表单提交信息
有四种提携带信息的方法:通过url中的token,通过form表单中的隐藏域(通过form表单),通过cookies,通过session。这四种方式可以用来保存一个会话状态。
补充一点:前端页面(jsp)请求URL时要加上contextPath,例如:${pageContext.request.contextPath}/login
Servlet中,转发(dispatcher)时,最开头的“/”会被系统自动翻译成contextPath,相当于去掉开头“/”,然后在前面拼接一个req.getContextPath()
请求转发:url不会发生变化,响应码:307
重定向:url会发生变化,相应码:302
<%--
Created by IntelliJ IDEA.
User: woshi
Date: 2020/3/23
Time: 11:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
login_index
package com.woshi.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginFormTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String hidden = req.getParameter("hidden");
System.out.println(username);
System.out.println(password);
System.out.println(hidden);
String duplicate = req.getParameter("duplicate");
String color = req.getParameter("color");
String letterCheck = req.getParameter("C");
System.out.println(duplicate);
System.out.println(color);
System.out.println(letterCheck);
}
}
admin
123123
hidden_value
1
蓝色
on