在mvc中保持两个不同的会话同一时间

时间:2014-09-05 11:05:35

标签: jsp session servlets model-view-controller

我正在使用mvc概念普通bean类概念,其中我有一个jsp页面在这里我被记录为不同的用户并将搜索选项放入其他人的详细信息并从数据库中提取所以从数据库中搜索一个人我们在我们的业务类它在控制台上执行并返回值但是我无法在jsp页面上获取这些值并且无法打印其获取null或打印我已经登录的现有用户详细信息是我的代码

我们正在获取输入的第一个jsp页面

 <html>
 <body>
 <%
 Employee emp1 = (Employee)session.getAttribute("emp1");
 session.setAttribute("emp1",emp1);
 %>
        <fieldset id="fieldhead">
        <legend>Check Employee Details</legend><BR>
        <table align=center cellspacing=1 cellpadding=3><form name ="form" method=post action="SearchEmployee">
            <center>
            <tr><Td>Enter keywords</td><TD><input name="data" ></td></tr>
            <tr><td><input type="submit"  value="SearchEmployee" />
           &nbsp;&nbsp;<button type=reset accesskey="R"><u>R</u>efresh</button></td></tr>
            </center>
        </table><BR>
        </fieldset>  
 </body>
 </html>

这是我的servlet代码,我正在接收值和调用业务类的功能

   @WebServlet(name = "SearchEmployee", urlPatterns = {"/SearchEmployee"})
    public class SearchEmployee extends HttpServlet {
    int z2;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     HttpSession session = request.getSession(true);    
     Employee emp1 = (Employee)session.getAttribute("emp1");
     emp1.setData(request.getParameter("data"));
     EmployeeBean eb = new EmployeeBean();
     List<Employee> search = eb.SearchEmployee(emp1);
     request.setAttribute("search", search);
     request.getRequestDispatcher("/SearchResult.jsp").forward(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
  }

 @Override
 public String getServletInfo() {
    return "Short description";
 }
 }

这是我从servlet调用的业务类函数

  public List<Employee> SearchEmployee(Employee emp1) {
  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  String p = emp1.getData();
  List<Employee> search = new ArrayList<Employee>();
  try {
      int count =0;
      con = ConnectionManager.getConnection();
      stmt = con.createStatement();
      String Query = "SELECT * FROM empinfo WHERE empName ='"+p+"' or fathername ='"+p+"' or  mailid = '"+p+"' or contactnum = '"+p+"' ";
      System.out.println("Query is " +Query);
       rs= stmt.executeQuery(Query);
      while (rs.next())
        {
     count++; 
       emp1 = new Employee();
    int qw = Integer.parseInt(rs.getString("empId"));
    String name = rs.getString("empName");
    System.out.println("name is " +name);
    System.out.println("qw is " +qw);
    emp1.setIid(Integer.parseInt(rs.getString("empId")));
    emp1.setName(rs.getString("empName"));
    search.add(emp1);
    System.out.println("............" +search);
    emp1.setValid(true);
        } System.out.println("records retrieved from the DB: "+count);
  } catch (SQLException  ex) {

  } finally {
      try {
          if (stmt != null) {
              stmt.close();
          }
          if (con != null) {
              con.close();
          }
      } catch (SQLException ex) {
          Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  return search;
  }

这是我最后一次jsp搜索结果

<html>
 <body>
 <%
 Employee emp1=(Employee)session.getAttribute("emp1");
 session.setAttribute("emp1",emp1);
 %>
    <h5>${message}</h5>
        <div align="center">
    <table border="1" cellpadding="3" width ="500px" >
        <caption><h2>List of Employee for leave Request</h2></caption>
            <tr>
            <th>employee ID</th>
            <th>Employee Name</th>
            </tr>
            <c:forEach items="${search}" var="item">
           <tr>
           <td>${item.getIid()}</td>  
           <td><a href ="SearchEmployee?data=${item.getName()}">${item.getName()}</a></td>
          </tr>
          </c:forEach>  
       </table>
      </div>
    </body>
  </html>

0 个答案:

没有答案