2016-06-15 63 views
2

LoginServlet.java值越來越插入數據庫,但他們不應該

package bean; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
public class LoginServlet extends HttpServlet { 
    @Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("text/html"); 
     try (PrintWriter out = response.getWriter()) { 
      request.getRequestDispatcher("link.html").include(request, response); 


      String name=request.getParameter("name"); 
      String password=request.getParameter("password"); 
      boolean status=false; 
    try{ 
     Connection con=ConnectionProvider.getCon(); 
     String sql="select * from roles where name='" + name + "' and pass='" + password + "'"; 
     PreparedStatement stmt =con.prepareStatement(sql); 
     String role="admin";       
     ResultSet rs=stmt.executeQuery(); 
     if(rs.next()) 
     { 
      status=true; 
      role=rs.getString("role"); 
     } 

     if(status){ 
     out.print("Welcome, "+name); 
     HttpSession session=request.getSession(); 
     session.setAttribute("name",name); 
     if(role!=null && role.equals("admin")){ 
      response.sendRedirect("create.html"); 

     } 
     else { 
      response.sendRedirect("create1.html");   

     } 

    } 
    else{ 
     out.print("Sorry, username or password error!"); 
     request.getRequestDispatcher("login.html").include(request, response); 
    } 
    }catch(SQLException | ServletException | IOException e){} 


     } 
} 
} 

create.html上

<a href="LogoutServlet">Logout</a> 
<a href="department.jsp">Create Department</a> 
<a href="c_user.jsp">Create Users</a> 
<hr/> 

department.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    <h1>Create Department</h1> 
<br> 
<form action="DepartmentServlet"> 
    <table border="1"> 
     <tbody> 
      <tr> 
       <td>Company Name :</td> 
       <td><input type="text" name="company" value="" size="50" /></td> 
      </tr> 
      <tr> 
       <td>Department Name</td> 
       <td><input type="text" name="department" value="" size="50" /> </td> 
      </tr> 
      <tr> 
       <td>Head Office :</td> 
       <td><input type="text" name="place" value="" size="50" /></td> 
      </tr> 

     </tbody> 
    </table> 
    <input type="reset" value="Clear" name="Clear" /> 
    <input type="submit" value="Submit" name="Submit" /> 
</form> 
</body> 
</html> 

DepartmentServlet.java

package bean; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 


public class DepartmentServlet extends HttpServlet { 

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html"); 
    try (PrintWriter out = response.getWriter()) { 
      request.getRequestDispatcher("link.html").include(request, response); 

      HttpSession session=request.getSession(false); 
      if(session!=null){ 
       String name=(String)session.getAttribute("name"); 
       boolean status=false; 
    try{ 
     String department=request.getParameter("department"); 
     String company=request.getParameter("company"); 
     String place=request.getParameter("place"); 

     Connection con=ConnectionProvider.getCon(); 
     String sql="insert into department(departmentname,company,place) values (?,?,?)"; 
     PreparedStatement pstmt =con.prepareStatement(sql); 

     pstmt.setString(1,department); 
     pstmt.setString(2,company); 
     pstmt.setString(3,place); 

     int rs=pstmt.executeUpdate(); 
     if(rs>0){status=true;} 
    }catch(Exception e){} 
       if(status){ 
       out.print("Values have been inserted,"+name); 
       request.getSession();} 
       else 
       { 
        out.print("failed"); 
       }     
       } 
      else{ 
       out.print("Please login first"); 
       request.getRequestDispatcher("login.html").include(request, response); 
      } 
     } 
} 
} 

LogoutServlet.java

package bean; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
public class LogoutServlet extends HttpServlet { 
      @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("text/html"); 
       try (PrintWriter out = response.getWriter()) { 
        request.getRequestDispatcher("link.html").include(request, response); 

        HttpSession session=request.getSession(false); 
        session.invalidate(); 

        out.print("You are successfully logged out!"); 
       } 
} 
} 

隨着DepartmentServlet,我將值插入到數據庫中。問題是,我能夠在沒有登錄的情況下打開create.htmldepartment.jsp,即使我沒有登錄,也會將值插入數據庫。我知道問題在於會話未正確傳遞(使用)。我該如何解決它?有人能糾正它嗎?

+1

牆的代碼問題不是對他人有用的未來,並且通常沒有得到很好的答案,而是創建一個[mcve]。 –

+1

'} catch(SQLException | ServletException | IOException e){}'可能很多重要的信息沒有在這裏打印 –

+0

好吧,但讓我等待約24小時,如果我不能得到任何滿意的答案我會發佈一個新的問題或編輯這一個 –

回答

0

你就必須實現過濾器來限制的.jsp/的.html訪問這將檢查活動的會話。如果那會沒有活動會話發現請求重定向到登錄頁面(在你的情況下link.Use下面執行doFilter方法的

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest request = (HttpServletRequest) req; 
     HttpServletResponse response = (HttpServletResponse) res; 
     HttpSession session = request.getSession(false); 

     if (session == null || session.getAttribute("name") == null) { 
      response.sendRedirect(request.getContextPath() + "/link"); 
     } else { 
      chain.doFilter(req, res); 
     } 
    } 
1

變化的條件if(session != null)
if(session != null && session.getAttribute("name") != null)

+0

謝謝你的作品 –

+0

另一個問題,我可以打開create.html和department.jsp沒有登錄直接輸入url可以檢查。 –

+0

@RahulGupta如果有效,請接受答案。 –