当前位置:首页 » 《随便一记》 » 正文

登录验证题目_abandon_i的博客

29 人参与  2022年04月17日 14:50  分类 : 《随便一记》  评论

点击全文阅读


CheckServlet.java

package servlet;

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;
import javax.servlet.http.HttpSession;

@WebServlet("/check")
public class CheckServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		
		String name=request.getParameter("name11");
		String password=request.getParameter("password11");
		
		if("admin".equals(name)&&"123".equals(password)) {
			HttpSession session=request.getSession();
			session.setAttribute("info",name);
			request.getRequestDispatcher("welcome").forward(request, response);
		}else {
			request.getRequestDispatcher("index.html").forward(request, response);
		}	
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

WelcomeServlet.java

package servlet;

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;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.print("<html>");
		out.print("<head>");
		out.print("</head>");
		out.print("<body>");
		HttpSession session=request.getSession();
		String info=(String)session.getAttribute("info");
		if(info==null) {
			request.getRequestDispatcher("index.html").forward(request, response);			
		}else {
			out.print("<h1>welcome</h1>");
			out.print("<h1>欢迎你来到青岛职业技术学院</h1>");
		}
		out.print("</body>");
		out.print("</html>");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>Insert title here</title>
</head>
<body>
<font size="5">
	<form action="check" method="post">
	用户名:<input type="text" name="name11"/><br>
	密码:<input type="text" name="password11"/><br>
	<input type="submit" value="登录"><input type="reset" value="重置"><br>
	</form>
</font>
</body>
</html>

这是文件的位置

  1.  添加到tomcat服务器上
  2. 运行服务器
  3. 在浏览器上输入http://127.0.0.1:8080/demo1104/index.html


点击全文阅读


本文链接:http://zhangshiyu.com/post/38267.html

这是  器上  重置  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1