0

大家好,我已经输入了一个查询数据库的页面,然后在数据库中输入了一些内容,但是当我去加载 servlet/jsp 页面时,它返回空白并且不确定发生了什么或为什么?我在 Eclipse 中编码,在查看控制台并没有打印出任何内容后,我认为我的代码有问题,但我看不到问题所在。

这是我的小服务程序

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TutorAssign
 */
@WebServlet("/TutorAssign")
public class TutorAssign extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TutorAssign() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    private void sendBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         HttpSession session = request.getSession(true);
            //Set data you want to send back to the request (will be forwarded to the page)
            //Can set string, int, list, array etc.

         String sql = "SELECT l.id,s.name,l.day,l.time,l.room" +
                  " FROM subject s, lab l " +
                    " WHERE s.user_id="+(Integer)session.getAttribute("id");

          try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("got boobs");
            System.out.println(session.getAttribute("id"));

              Statement stmt = con.createStatement();
              ResultSet res = stmt.executeQuery(sql);
              System.out.println(res);
              ArrayList<String> list1 = new ArrayList<String>();
              ArrayList<String> list2 = new ArrayList<String>();
              if (res.next()){
                  do{
                       list1.add(res.getString(1));
                       list2.add(res.getString(2)+" "+res.getString(3)+" "+res.getString(4)+" "+res.getString(5));


                  }while(res.next());
              System.out.println("Outside");
              String[] arr1 = list1.toArray(new String[list1.size()]);
              String[] arr2 = list2.toArray(new String[list2.size()]);
              System.out.println(list1);
              request.setAttribute("res1", arr1);
              request.setAttribute("res2", arr2);

              }



          }catch (SQLException e) {
            } 
            catch (Exception e) {
            } 


            //Decides what page to send the request data to
            RequestDispatcher view = request.getRequestDispatcher("TutorAssign.jsp");
            //Forward to the page and pass the request and response information
            view.forward(request, response); 
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


          int user_id = Integer.parseInt(request.getParameter("id"));
          int lab_id = 0;

          String message = null; 
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("got connection"); 
            System.out.println(user_id);
            Statement s = con.createStatement(); 

            String sql = "INSERT INTO user_lab" + 
                      " (user_id, lab_id)" + 
                      " VALUES" + 
                      " ('" +  user_id + "'," + 
                         " '"  +  lab_id + "')"; 

              System.out.println(sql);
              int i = s.executeUpdate(sql); 
              if (i==1) {
                message = "Successfully assigned a tutor."; 
                response.sendRedirect("Lecturer_labs");
              } 

              s.close(); 
              con.close(); 
            } 
            catch (SQLException e) {
              message = "Error." + e.toString(); 
              boolean error = true; 
            } 
            catch (Exception e) {
              message = "Error." + e.toString(); 
              boolean error = true; 
            } 
            if (message!=null) {
              PrintWriter out = response.getWriter(); 
              out.println("<B>" + message + "</B><BR>"); 
              out.println("<HR><BR>"); 
            } 

          } 
// TODO Auto-generated method stub


}
4

2 回答 2

1

我注意到您的 doGet 方法尚未实现。我会假设您的 sendBack 代码没有被调用

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   sendBack(request,response);
}
于 2012-10-06T11:27:35.057 回答
0

你有几个空的 catch 块。这意味着如果程序遇到问题,错误将被默默吞下,你不知道执行了哪些代码,跳过了哪些代码,因此结果未知。至少在 catch 块中记录错误,像这样(非常基本的示例,使用java.util.logging

} catch (SQLException e) {
    logger.log(Level.INFO,"SQL Error encountered",e);        
} catch (Exception e) {
    logger.log(Level.INFO,"Other error encountered",e);        
}

从正确处理错误开始,并研究程序遇到的错误。

除了这个问题,正如其他人指出的那样,你doGet的没有实现 - 所以如果你用它来调用页面,GET它不会做任何事情,因为doGet它是空的。它应该做点什么POST

于 2012-10-06T11:27:22.403 回答