【Eclispe】JavaWeb——servlet


login.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>登录</title>
</head>
<body>
<form action="loginCheck" method="post" >
请输入用户名:<input type="text" name="usename"><br/>
请输入密码:<input type="password" name="usepwd"><br/>
<input type="submit" value="登入">
<input type="reset">
</form>
</body>
</html>

Hello.java

package test;

import java.io.IOException;
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 Hello
 */
@WebServlet("/loginCheck")
public class Hello extends HttpServlet {
    
    
	private static final long serialVersionUID = 1L;
    public Hello() {
    
    
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		String name=request.getParameter("usename");
		String pwd=request.getParameter("usepwd");
		String ifo="";
		if("abc".equals(name)&&"123".equals(pwd))
		{
    
    
		 ifo="欢迎你";
		}
		else
		{
    
    
			ifo="不欢迎你";
		}
		request.setAttribute("out", ifo);
		request.getRequestDispatcher("/info.jsp").forward(request, response);
	}

}

info.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>输出页面</title>
</head>
<body>
<%=request.getAttribute("out") %>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_52115728/article/details/124605301