Click here to Skip to main content
15,889,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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

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
HTML
<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
HTML
<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
HTML
<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>
Posted
Updated 30-Nov-13 11:26am
v2

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?
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900