当前位置: 代码迷 >> Java Web开发 >> jsp有控制验证的功能吗?解决方法
  详细解决方案

jsp有控制验证的功能吗?解决方法

热度:59   发布时间:2016-04-17 14:49:35.0
jsp有控制验证的功能吗?
jsp有控制验证的功能吗?
就像asp.net一样,可以控制一个文件夹中的内容,只有通过登陆才能访问,在.net中是在web.config配置的。不知道java有没有
说的具体些

------解决方案--------------------
Java 里是在web.xml配置。好像。
用Filter可以很好的控制。具体可以去搜一下。很多的。
下面是我的一个练手项目中的代码,你参考一下吧。很多代码是IDE生成的。我使用Java Studio Enterprise
8。

web.xml 部分配置

<filter>
<filter-name> VerifyAdminFilter </filter-name>
<filter-class> com.ustb.lab.businessLogic.VerifyAdminFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> VerifyAdminFilter </filter-name>
<url-pattern> /cn/mng/* </url-pattern>
</filter-mapping>
------解决方案--------------------
VerifyUserFilter.java
内容太长了。发表不了。给出代码片段吧:

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain)

throws IOException, ServletException {



if (debug) log( "VerifyUserFilter:doFilter() ");



doBeforeProcessing(request, response);



Throwable problem = null;



try {

HttpServletRequest hrequest = (HttpServletRequest)request;

HttpServletResponse hresponse = (HttpServletResponse)response;



HttpSession loginsession = hrequest.getSession(false);



if (loginsession.getAttribute( "LogInlet.userName ") == null) {

//out.println( "please log in before trying to use this facility , click " + " <a href= \ "index.jsp\ "> click here to login! </a> ");

System.out.println( "Filter rejected, please go to the sign on page ");



hresponse.sendRedirect( "clublogin.jsp "); // go to the sign on page

}else{

chain.doFilter(request, response);

}

} catch(Throwable t) {

//

// If an exception is thrown somewhere down the filter chain,

// we still want to execute our after processing, and then

// rethrow the problem after that.

//

problem = t;

t.printStackTrace();

}



doAfterProcessing(request, response);



//

// If there was a problem, we want to rethrow it if it is

// a known type, otherwise log it.

//

if (problem != null) {

if (problem instanceof ServletException) throw (ServletException)problem;

if (problem instanceof IOException) throw (IOException)problem;

sendProcessingError(problem, response);

}

}
------解决方案--------------------
LogInlet.java (调用代码)

片段:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType( "text/html;charset=UTF-8 ");

request.setCharacterEncoding( "UTF-8 ");
  相关解决方案