目录结构

src 目录下com.xieyuan包MyServlet.java文件(Servlet文件)

package com.xieyuan;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Random; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.sun.corba.se.impl.javax.rmi.CORBA.Util;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public class MyServlet extends HttpServlet { /**
* Constructor of the object.
*/
public MyServlet() {
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 {
request.setCharacterEncoding("UTF-8");
response.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>请登录查看NOTICE文件</TITLE></HEAD>");
out.println("<style>body,td,div{font-size:12px;}</style>");
out.println("<BODY>"); String referer=request.getHeader("referer");
//通过获取用户上次进入的页面判断用户是否输入过密码,然后跳转到本页来的
if(referer!=null&&referer.equals(request.getRequestURL().toString()))
out.println("<font color='red'>账号密码错误!</font>");
out.println("<form action='"+request.getRequestURI()+"' method='post'>");
out.println("账号:<input type='text' name='username' autocomplete='off' style='width:200px' /><br/>");
out.println("密码:<input type='password' name='password' style='width:200px' /><br/>");
out.println("<input type='submit' value='登录' />");
out.println("</form>"); //获取上下文参数
out.println("<BR/>"+ this.getServletContext().getInitParameter("test"));
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 {
//获得提交的数据
String userName=request.getParameter("username");
String password=request.getParameter("password");
//遍历所有初始参数参数
Enumeration<?> params=this.getInitParameterNames();
while(params.hasMoreElements())
{
String paramName=(String)params.nextElement();
String paramValue=this.getInitParameter(paramName);
if(paramName.equals(userName)&¶mValue.equals(password))
{
/*
* 一个RequestDispatcher对象作为资源在指定的路径,或者为null的包装,
* 如果servlet容器不能返回的RequestDispatcher
*
将请求转发到另一个资源(servlet,JSP和文件或HTML文件)从一个servlet在服务器上。
此方法允许一个servlet做初步处理的请求和其他资源来生成响应。 */
request.getRequestDispatcher("/WEB-INF/notice.txt").forward(request, response);
return;
} }
//不匹配,返回
doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
} }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>MyServlet</servlet-name>
<servlet-class>com.xieyuan.MyServlet</servlet-class>
<init-param>
<param-name>user123</param-name>
<param-value>pass123</param-value>
</init-param> </servlet> <context-param>
<param-name>test</param-name>
<param-value>这是测试数据!</param-value>
</context-param> <servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

部署好WEB后,访问:http://127.0.0.1:8080/Test/servlet/MyServlet 进入登录页面

输入正确的账号:

user123

密码

pass123

访问到预先定义的WEB-INF/notice.txt页面

Servlet:通过初始参数实现权限访问某个文件、页面的更多相关文章

  1. AD域-让共享目录只显示用户有权限访问的文件夹

    问题: 在AD域中,我们一般都会用到共享,如果有很多部门,我们可能还会按部门.职位配置权限.比如CSD,IT,PA等,但文件夹一多,用户看着就头大,而且用户没权限访问的文件夹误点击进去还会提示无权限访 ...

  2. “打开ftp服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹"

    阿里云虚拟主机上传网站程序 问题场景:网页制作完成后,程序需上传至虚拟主机 注意事项: 1.Windows系统的主机请将全部网页文件直接上传到FTP根目录,即 / . 2. 如果网页文件较多,上传较慢 ...

  3. 打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹

    打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问 在win98,winme,win2000,win2003下都能正常上传文件夹,但在winxp+sp2下同样的文件夹就可能出现问题 1. 打开 ...

  4. windows server 打开 FTP 服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。

    解决方案1: 打开高级安全windows防火墙,设置出入站规则. 然后,再打开windows防火墙界面,点击左上角“允许程序或功能通过windows防火墙”,勾选上设置的出入站名称和FTP服务器. 如 ...

  5. 安装完Apache和PHP之后访问PHP文件页面提示下载而没有解析 解决办法

    装好LAMP环境后,还要做下整合Apache与PHP相关配置,在apache配置文件里找到:AddType application/x-gzip .gz .tgz在该行下面添加AddType appl ...

  6. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  7. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  8. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  9. Servlet之初始化参数和传递数据(ServletConfig,ServletContext )

    ServletConfig 容器初始化一个Servlet的时候,会为这个Servlet建一个唯一的Servletconfig的对象(Servlet的配置对象) 容器会从部署的描述文件(web.xml) ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之1042字符串比较

          题目 解决代码及点评 /********************************************************************** ...

  2. [poj 1904]King's Quest[Tarjan强连通分量]

    题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...

  3. POJ - 1422 Air Raid 二分图最大匹配

    题目大意:有n个点,m条单向线段.如今问要从几个点出发才干遍历到全部的点 解题思路:二分图最大匹配,仅仅要一条匹配,就表示两个点联通,两个点联通仅仅须要选取当中一个点就可以,所以有多少条匹配.就能够减 ...

  4. 怎样在ios开发中设置tableview的cell颜色

    //方法一: cell .contentView .backgroundColor = [ UIColor redColor ]; //方法二: UITableViewCell *cell = [ta ...

  5. HttpWebRequest 基础连接已经关闭: 接收时发生错误

    HttpWebRequest request = null; Stream webStream = null; HttpWebResponse response = null; StreamReade ...

  6. maven生成war包的两种方式

    war包即对WEB应用程序进行打包,用于应用容器的部署.如在jboss中只要把war包丢入deploy目录下即可发布自己的应用了.打包方式有很多中,很多工具本身就支持此功能.下面主要介绍通过maven ...

  7. MySQL数据库触发器(trigger)

    MySQL触发器(trigger):监视某种情况并触发某种操作 一:四要素 触发时间:before/after 地点:table 监视操作:insert/update/delete 触发操作:inse ...

  8. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

  9. CodeForces 370C. Mittens

    C. Mittens time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  10. Mac Eclipse+Maven+TestNg+ReportNg 生成测试报告

    TestNG 是java 的单元测试框架,功能很强大,很方便,但是自动生成的测试报告有待改善,可以使用TestNg 自带的TestNG_xslt更改TestNG报告的样式,这里主要讲解ReportNg ...