JSP代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--action表示提交至服务器哪个资源 method表示以什么方式提交-->
<form action="doLog" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
jsp调用doLog
package Serv;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class doLog
*/
@WebServlet("/doLog")
public class doLog extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String name=null;
/**
* @see HttpServlet#HttpServlet()
*/
public doLog() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置文档类型 字符集
response.setContentType("text/html;charset=utf-8");
//获取输出流
PrintWriter out=response.getWriter();
request.setCharacterEncoding("utf-8");
//获得客户端参数
String uname=request.getParameter("username");
name=uname;
String upwd=request.getParameter("password");
//请求重映像时路径区分大小写----相对路径
//请求重映像时路径区分大小写----相对路径
//请求重映像时路径区分大小写----相对路径
//response.sendRedirect("DoWelcome");
//请求重映像时路径区分大小写----绝对路径
//不能使用equals会发生空指针异常
//使用
if (uname.equals("admin")&&upwd.equals("123")){
response.sendRedirect("DoWelcome");
} else {
response.sendRedirect("login.jsp");
}
// if (!uname.equals("admin")||!upwd.equals("123")){
// response.sendRedirect("login.jsp");
// } else {
// response.sendRedirect("DoWelcome");
// }
//response.sendRedirect(request.getContextPath()+"/DoWelcome");
out.close();
}
public String Getname() {
return name;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
欢迎界面
package Serv;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DoWelcome
*/
@WebServlet("/DoWelcome")
public class DoWelcome extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DoWelcome() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
doLog d=new doLog();
String uname=d.Getname();
out.println("Welcome "+uname);
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
登陆界面
输入用户信息
输入正确
输入错误