1

我在 Google gcm 应用程序中工作,在这里我通过正确的 ID 和密码对应用程序用户进行身份验证。身份验证工作正常。

我正在通过 Run as -> Run on Server(Homeservlet.java) 运行此页面,即使对于正确的员工和密码,它也没有显示书面的 jsp 代码(在 if 条件中编写)并转到 else -部分。

在 Eclipse 控制台中:我可以看到员工姓名和密码。但我的问题是如何设置这些值,以便当我运行这个页面时它会显示里面的那个 jsp 页面。

我正在使用 set 参数来设置值,但是每当我在 Tomcat 服务器中运行此页面时,它都会显示IllegalArgumentException。我发现它很安静,因为当我运行时,没有设置值。

其实我想要的是正确的员工和相应的密码,......它会显示那个jsp页面;否则(我的意思是在其他部分,它不会)

public class HomeServlet extends BaseServlet {

      static final String ATTRIBUTE_STATUS = "status";
      private static final int HTTP_STATUS = 200;
    //  private static final String HTTP = "OK";


      protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException {


          PreparedStatement stmt = null;
          String employee=req.getParameter("employeeid"); //getting the value from app User
          String password=req.getParameter("password");  //corresponding password
          req.setAttribute(employee, employee);
          req.setAttribute(password, password);

          try {
              String url="jdbc:mysql://localhost/apps";
              Class.forName("com.mysql.jdbc.Driver");
              Connection con=DriverManager.getConnection(url,"root","root");
              stmt = con.prepareStatement("select * from regid where emp_id=? and password=?");
              stmt.setString(1, employee);
              stmt.setString(2, password);
              ResultSet rs = stmt.executeQuery();

              if(rs.next()) {
                  System.out.println("2> Employee Id : "+employee+" && Password : "+password);
                  System.out.println("3> This employee "+employee+" exsists in the database and will be there");      

                  resp.setContentType("text/html");
                  PrintWriter out = resp.getWriter();
                  out.print("<html>");      //1> want to run this portion from here
                  out.print("<head>");
                  out.print("<title>Policy Page</title>");
                  out.print("<link rel='icon' href='../images/favicon.png'/>");
                  out.print("</head>");
                      out.print("<body>");
                  String status = (String) req.getAttribute(ATTRIBUTE_STATUS);
                  if (status != null)
                  {
                    out.print("Status : "+status);
                  }
                  List<String> devices = Datastore.getDevices();
                  if (devices.isEmpty())
                  {
                    out.print("<h2>No  devices registered!</h2>");
                  } 
                  else
                  {

                   out.print("<h2>" + devices.size() + " device(s) registered!</h2>");
                   out.print("<form name='form' method='POST' action='sendAll'>");
                   out.print("<input type='text' name='policy'>");
                   resp.setStatus(HttpServletResponse.SC_OK);
                   out.print("<input type='submit' value='Apply Policy'>");
                   out.print("</form>");
//                 getServletContext().getRequestDispatcher("/home").forward(req, resp);

                  }
                  out.print("</body></html>");   //2> to here
                  resp.setStatus(HttpServletResponse.SC_OK);

              }


              else {                                      //else-part
                  resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                  System.out.println(HttpServletResponse.SC_BAD_REQUEST);
                  System.out.println("4> This employee "+employee+" does not exsist in the database");            
              }
          }
          catch(Exception e) {
              e.printStackTrace();
          }
          finally {
              try {
                  stmt.close();
              } catch(Exception x) {}
          }


      }

      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws IOException {
        doGet(req, resp);
      }

    }

当应用程序用户提供 id-password 时,控制台中的输出为:

2> Employee Id : P1 && Password : ppp
3> This employee P1 exsists in the database and will be there

但我正在运行页面(运行为->在 server-tomcat-6 上运行),它显示了这个(而不是显示 jsp 页面)

HTTP Status 500
java.lang.IllegalArgumentException: Cannot call setAttribute with a null name
    at org.apache.catalina.connector.Request.setAttribute(Request.java:1431)
    at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:50

任何想法.......我哪里出错了。

4

1 回答 1

1

观察到 2 件事。

1) 使用

  req.setParameter("employee", employee);
   req.setParameter("password", password);

反而

  req.setAttribute(employee, employee);
  req.setAttribute(password, password);

2)

您显示的下一页不是 JSP。它是在 servlet 中创建的纯 html。设置的内容类型为html。

如果你想在 html 中显示员工,你可以写这样的代码,

 out.print("<body>");
 out.print("Welcome to this site Mr."+ employee);

如果您仍想将员工用作该 html 上的变量,则必须在此页面中嵌入 Javascript。

于 2012-10-01T10:47:03.280 回答