如何使用RequestDispatcher登录和转发页面后创建会话 [英] how to create session after login and forwarding page using RequestDispatcher

查看:69
本文介绍了如何使用RequestDispatcher登录和转发页面后创建会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在验证用户后创建会话,如果登录有效则创建sesssion但我的代码在加载登录jsp页面时创建会话。我希望在使用RequestDispatcher登录到另一个jsp页面后转发页面,但页面重定向到LoginServlet。给我任何解决方案。我的代码在下面

LoginServlet.java

I want to create session after authenticating the user, if login is valid then create the sesssion but my code is creating session on loading the login jsp page. And i want to forward the page after login to another jsp page using RequestDispatcher but page is redirecting to the LoginServlet. Give me any solution about it. My code is Below
LoginServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String uname = request.getParameter("uname");
		String pass = request.getParameter("pass");
		
		if(uname == "junaid" && pass == "1012109"){
			HttpSession session = request.getSession();
			session.setAttribute("username", uname);
			RequestDispatcher rd = request.getRequestDispatcher("/Welcome.jsp");
			rd.forward(request, response);
		}else{
			out.print("username/password invalid...");
			RequestDispatcher rd = request.getRequestDispatcher("/LoginPage.jsp");
			rd.include(request, response);
		}
	}



SessionListener.java




SessionListener.java

    private int session_count = 0;
    public void sessionCreated(HttpSessionEvent arg0) {

    session_count++;
    System.out.println("Session Added. Total Session: "+session_count);
}
public void sessionDestroyed(HttpSessionEvent arg0) {
    session_count--;
    System.out.println("Session Detroyed. Total Session: "+session_count);
    // TODO Auto-generated method stub
}





LoginPage.jsp



LoginPage.jsp

<body>
<form method="post" action="/sessioncounter2/LoginServlet">
Username <input type="text" id="uname" name="uname" /> <br/>
Password <input type="text" id="pass" name="pass" /> <br/>
<input type="submit" value="Login" />
</form>
</body>



pom.xml


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pk.edu.zab</groupId>
  <artifactId>sessioncounter2</artifactId>
  <packaging>war</packaging>
  <version>1.2-SNAPSHOT</version>
  <name>sessioncounter2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>servlet-api</artifactId>
    	<version>2.5</version>
    	<scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>sessioncounter2</finalName>
    <pluginManagement>
    	<plugins>
    		<plugin>
    			<groupId>org.apache.tomcat.maven</groupId>
    			<artifactId>tomcat7-maven-plugin</artifactId>
    			<version>2.1</version>
    			<configuration>
    	<port>108</port>
    			</configuration>
    		</plugin>
    	</plugins>
    </pluginManagement>
  </build>
</project>





web.xml



web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <listener>
  	<listener-class>pk.edu.zab.SessionListner</listener-class>
  </listener>
  <servlet>
  	<servlet-name>LoginServlet</servlet-name>
  	<display-name>LoginServlet</display-name>
  	<description></description>
  	<servlet-class>pk.edu.zab.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>LoginServlet</servlet-name>
  	<url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
  	<welcome-file>LoginPage.jsp</welcome-file>
  </welcome-file-list>
</web-app>

推荐答案

thanx兄弟,但我已经解决了我的问题。我对在web.xml中声明filter-pattern感到困惑。 url-pattern定义将使用过滤器的任何页面,filter-pattern中的servlet-name定义将使用此过滤器的servlet。但是为什么我们使用它以及我们可以通过声明它们来做什么?
thanx bro but i have solved my problem. I am little confuse about declaring filter-pattern in web.xml. url-pattern defines any page that will use the filter and servlet-name in filter-pattern defines the servlet that will use this filter. But why we use this and what we can do by declaring them?


http://docs.oracle.com/javaee/1.4/api/javax/jms/Session.html 详细阅读


这篇关于如何使用RequestDispatcher登录和转发页面后创建会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆