servlet从xml提取数据登陆
在xml文档下可以设置参数的初始值,在这里把他当成了数据库在用

<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>InitParamServlet</servlet-name> <servlet-class>servlet.InitParamServlet</servlet-class> <init-param> <param-name>helloween</param-name> <param-value>password</param-value> </init-param> <init-param> <param-name>admin</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>babyface</param-name> <param-value>babyface</param-value> </init-param> </servlet>
变量名为账号,值为密码
在doGet中的<form>提供的<input type='text' name='username'...>的值
可以传递到doPost
然后用String username=request.getParameter("username");来获取他
再和xml里面的变量比较
原码

package servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InitParamServlet extends HttpServlet { /** * Constructor of the object. */ public InitParamServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<form action='"+request.getRequestURI()+"' method='post'>"); out.println("账号:<input type='text' name='username'<br><br/>"); out.println("密码:<input type='password' name='password'<br><br/>"); out.println("<input type='submit' value=' 登陆 '>"); out.println("</form>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username=request.getParameter("username"); String password=request.getParameter("password"); Enumeration params=this.getInitParameterNames(); for(;params.hasMoreElements();){ String usernameParam=params.nextElement().toString(); String passnameParam=getInitParameter(usernameParam); if(usernameParam.equalsIgnoreCase(username)&&passnameParam.equals(password)){ request.getRequestDispatcher("/WEB-INF/notice.html").forward(request,response); return; } } doGet(request,response);//不匹配的话重新加载 } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步