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 { @Override protected 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>"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自动生成的方法存根 doGet(req, resp); } }
html代码:(html1)
Html1.html #home { width height margin .dl1{ clear .dl1 dt{ float .dd1 dd{ float h1{ 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>");