Unable to set the value for running the java file

时光总嘲笑我的痴心妄想 提交于 2019-12-14 02:28:25

问题


I'm working in Google gcm application,and here I'm authenticating the app user by correct Id & password.Authentication is working properly.

My I'm running this page by Run as -> Run on Server(Homeservlet.java),even for the correct employee and password,it's not showing the written jsp code(which is written in the if condition) and going to the else-part.

In the eclipse console : I can see the employee name and it's password.But my question is how to set the values sothat when I will run this page it'll show that jsp page inside.

I'm using set parameter to set the value,but whenever I'm running this page in Tomcat server,it's showing IllegalArgumentException.I found it's quiet relevant because when I'm running the value's are not set.

Actually I want ,for the correct employee and corresponding password,...it'll show that jsp page; otherwise(i mean in else-part,it'll not)

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);
      }

    }

When the app user giving the id-password,the output in the console is:

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

but I'm running the page(run as->run on server-tomcat-6),it is showing this(instead of showing the jsp page)

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

any idea....... where I'm going wrong.


回答1:


2 things observed.

1) Use

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

instead

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

2)

The next page you are showing is not a JSP. It is plain html created in servlet. The set content type is html.

If you want to display employee in html, you can write code like this,

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

If you still want to use the employee as a variable on that html, you have to embed Javascript in this page.



来源:https://stackoverflow.com/questions/12671563/unable-to-set-the-value-for-running-the-java-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!