Java Web学习笔记3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Web学习笔记3相关的知识,希望对你有一定的参考价值。
今天做了一个实验:Servlet访问WEB-INF目录下的文件notice.html
最后始终不能出现预期的效果,我猜想可能是使用了Tomcat 8版本的原因吧,暂时放下,等以后知识丰富了,再来解决它.
目前的效果如下:
代码及配置如下:
package com.helloxr.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; /* * Servlet访问WEB-INF目录下的文件notice.html */ public class InitParamServlet extends HttpServlet { private static final long serialVersionUID = 6272811112212L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); // 设置request编码方式 response.setCharacterEncoding("UTF-8"); // 设置response编码方式 response.setContentType("text/html"); // 设置类型为HTML类型 PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>请登录后查看 Notice 文件</TITLE>"); out.println(" <style>body,td,div {font-size:12px; }</style>"); out.println(" </HEAD>"); out.println(" <BODY>"); out.println("<form action=‘" + request.getRequestURI() + "‘ method=‘post‘>"); out.println("账号: <input type=‘text‘ name=‘username‘ style=‘width:200px; ‘> <br/><br/>"); out.println("密码: <input type=‘password‘ name=‘password‘ style=‘width:200px; ‘> <br/><br/>"); out.println("<input type=‘submit‘ value=‘ 登录 ‘>"); out.println("</form>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); // 提交的usernmae参数 String password = request.getParameter("password"); // 提交的password参数 Enumeration<String> params = this.getInitParameterNames(); // 所有的初始化参数名称 while(params.hasMoreElements()){ String usernameParam = (String)params.nextElement(); // 参数名,即用户名 String passnameParam = getInitParameter(usernameParam); // 参数值,即密码 if(usernameParam.equals(username) && passnameParam.equals(password) ){ // 如果用户名及密码都匹配则显示 request.getRequestDispatcher("/WEB-INF/notice.html").forward(request,response); return; } } this.doGet(request, response); // username, password不匹配,显示登录页面 } }
web.xml文件相关内容:
<servlet> <servlet-name>InitParamServlet</servlet-name> <servlet-class>com.helloxr.servlet.InitParamServlet</servlet-class> <init-param> <param-name>helloxr</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>
<servlet-mapping> <servlet-name>InitParamServlet</servlet-name> <url-pattern>/servlet/InitParamServlet</url-pattern> </servlet-mapping>
以上是关于Java Web学习笔记3的主要内容,如果未能解决你的问题,请参考以下文章