编写Servlet,验证用户登录,如果用户名与密码都

      2020-08-01 18:27      HTML5

java代码:(Test1)

package com.test;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;@WebServlet(urlPatterns={"/Test1"})public class test1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO 自动生成的方法存根req.setCharacterEncoding("gbk");resp.setContentType("text/html;charset=utf-8");PrintWriter out = resp.getWriter();String user=req.getParameter("user");String pass = req.getParameter("pwd");if(user.equals("admin")&&pass.equals("admin")){out.println("欢迎登陆");}else{out.println("<script language='javascript'>alert('用户名或密码错误,请重新输入!')</script>");out.println("<script language='javascript'>window.location.href='/Servlet_work/Html1.html'</script>");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO 自动生成的方法存根doGet(req, resp);}}

html代码:(html1)

Html1.html#home {widthheightmargin.dl1{clear.dl1 dt{float.dd1 dd{floath1{text-align#foot{text-align用户登陆用户账号:用户密码:

运行截图:

备注:

在Servlet中实现页面中弹窗:

PrintWriter out = resp.getWriter(); out.println("<script language='javascript'>alert('用户名或密码错误,请重新输入!')</script>");

在Servlet中实现页面的跳转:

out.println("<script language='javascript'>window.location.href='/Servlet_work/Html1.html'</script>");