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
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);
}
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
<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-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>