1

私は Google gcm アプリケーションで作業しています。ここでは、正しい ID とパスワードでアプリ ユーザーを認証しています。認証は正常に機能しています。

私はRun as -> Run on Server(Homeservlet.java)でこのページを実行していますが、正しい従業員とパスワードであっても、書かれたjspコード(if条件で書かれています)が表示されず、elseに行きます-部。

Eclipse コンソールで: 従業員の名前とパスワードを確認できます。しかし、私の質問は、このページを実行したときにその jsp ページが内部に表示されるように値を設定する方法です。

set parameter を使用して値を設定していますが、Tomcat サーバーでこのページを実行しているときはいつでも表示IllegalArgumentExceptionされます。

実際には、正しい従業員と対応するパスワードが必要です...そのjspページが表示されます。それ以外の場合(つまり、else-partでは、そうではありません)

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 ではありません。サーブレットで作成されたプレーンな html です。設定されているコンテンツ タイプは html です。

html で従業員を表示したい場合は、次のようなコードを記述できます。

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

その html で従業員を変数として使用したい場合は、このページに Javascript を埋め込む必要があります。

于 2012-10-01T10:47:03.280 に答える