java - 无法设置运行java文件的值

标签 java jsp servlets google-cloud-messaging

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

我通过运行方式 -> 在服务器上运行(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 密码时,控制台中的输出为:

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

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

最佳答案

观察到 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。

关于java - 无法设置运行java文件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671563/

相关文章:

java - 使用Java的数据库中的非英文字符

java - 单击按钮时如何调用servlet

java - apache commons-fileupload 是否总是必须写入磁盘?

servlets - 如何将 servlet 动态添加到 jetty 服务器?

java - Tomcat 上的简单 Java Web 应用程序

java - 如何使用线程在不同系统中运行同一个java项目

java - iCal4J 错误 : net. fortuna.ical4j.util.Configurator <clinit>

java - LocalDateTime 到特定时区

Java JSP getParameter显示空字

javascript - 如何将字符串变量传递给 JSP 中的 javascript onClick 函数