Earlier my code was working but now I am facing this error
Sometimes the code works and sometimes it does not work.
My code is
Java4sController.java
package java4s;
import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.sql.*;
//import org.h2.jdbcx.JdbcConnectionPool;
//import org.springframework.security.core.context.SecurityContextHolder;
@Controller
public class Java4sController {
HttpSession session;
String uname="",pass="";
Statement stmt;
Connection conn;
@RequestMapping(value="displayForm.html", method=RequestMethod.GET)
public String helloWorld(UserDetails ud,HttpServletRequest request,HttpServletResponse response) {
return "loginPage";
}
@RequestMapping("/login")
public String loginCheck(@Valid UserDetails userDetails, BindingResult result, ModelMap model,HttpServletRequest request,HttpServletResponse response)throws Exception
{
System.out.println("In the login page");
String username=userDetails.getUser();
String password=userDetails.getPassword();
if (result.hasErrors()) {
return "loginPage";
}
try {
Class.forName("org.h2.Driver");
conn = DriverManager.
getConnection("jdbc:h2:file:C:/Users/patilv/test", "sa", "");
// add application code here
if(conn!=null) {
System.out.println("No connection");
}
stmt=conn.createStatement();
String sql="Select * FROM table1 WHERE username='"+username+"' AND password='"+password+"' ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
uname=rs.getString("username");
pass=rs.getString("password");
if (result.hasErrors()) {
return "loginPage";
}
else if(username.equals(uname)&&password.equals(pass))
{
//session.invalidate();
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma","no-cache");
session = request.getSession(); // create session
session.setAttribute("username", username);
session.setAttribute("password", password);
//String str1=(String)session.getAttribute(username);
model.addAttribute("lfobj", userDetails);
//model.addAttribute(str1);
conn.close();
return "success";
}
else
{
conn.close();
return "loginPage";
}
}
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
return "abcd";
}
@RequestMapping(value="logoutform", method=RequestMethod.GET)
public String helloWorld1(UserDetails ud,HttpServletRequest request,HttpServletResponse response) {
System.out.println("
abcdef
");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma","no-cache");
String username=ud.getUser();
// String password=ud.getPassword();
HttpSession session1;
session1=request.getSession(false);
System.out.println("Session :"+session1.getAttribute("username"));
if(session1!=null)
{
//session1.removeAttribute(username);
session1.invalidate();
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
cookie.setMaxAge(0);
cookie.setValue(null);
cookie.setPath("/");
response.addCookie(cookie);
}
System.out.println("invalidating session");
//session1.invalidate();
}
return "logoutsuccess";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<servlet>
<servlet-name>java4s</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>java4s</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
java4s-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="java4s" />
<mvc:annotation-driven />
<!-- <mvc:resources mapping="/scripts/**" location="/WEB-INF/" /> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="props" />
</bean>
</beans>
My directory structure is
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…