servlet中的会话构建错误

时间:2016-06-10 05:32:29

标签: java jsp session servlets

会话中存在问题。 在create.html中,当我点击createdepartment时,createdepartment.jsp页面打开我在提交数据后复制其url1,我再次复制url2然后我退出后退出时我在浏览器中粘贴url2它给出的消息请登录首先打开login.html,但是当我在浏览器中粘贴url1时,它会打开它,但它不应该。为什么会这样? 我已经给出了代码,请有人纠正吗?

LoginServlet.java

package bean;

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

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") ){   

        request.getRequestDispatcher("create.html").include(request, response);

        }
        else {

         request.getRequestDispatcher("create1.html").include(request, response);

      }

    }
    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");
              }                    
                request.getRequestDispatcher("department.jsp").include(request, response);
            }
            else{
                out.print("Please login first");
                request.getRequestDispatcher("login.html").include(request, response);
            }
        }
    }

    }

Logout.Servlet

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();
                    session.invalidate();

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

3 个答案:

答案 0 :(得分:0)

LogoutServlet.java代码可能有问题。

你还没有提到它的代码。

退出程序包括两件重要的事情......

  1. 删除附加到该会话对象的所有属性
  2. 使会话对象为null(通过创建session.invalidate();) 这样做会使会话符合垃圾收集条件,并删除对象(存储在堆内存中的对象)的引用(引用存储在堆栈中)。
  3.   

    在你的情况下,我猜(因为LogoutServlet.java不存在),很可能出错的原因是   &#34; 您可能正在创建一个新的会话对象,然后您正在复制旧会话对象的引用,现在您将通过&#34; session.invalidate();&删除此新会话;& #34;

         

    上述过程删除了新的引用,但旧的引用仍然存在,因为新对象中的可用副本被删除而不是原始副本。

    有关更具体和正确的答案,请附上您的LogoutServlet.java源代码。

答案 1 :(得分:0)

你的注销servlet应该有request.getSession(false)..如果一个已经存在,这将不会返回新的会话。在运行之前清除你的浏览器缓存。

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

答案 2 :(得分:0)

http://mytechsoft.jimdo.com/exclusive-projects

  

以上链接是针对您的问题,请参阅其中的前2张照片