1

I implement client server code with java android. the problem is I couldn't be able to connect to servlet. Where is my problem? Here is my code:

android code: ....

HttpClient client=new DefaultHttpClient();
HttpPost getMethod=new HttpPost("http://" + Server + "/RestaurantServer/Login");

try {
    // Add your data
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Mode", "Login"));
nameValuePairs.add(new BasicNameValuePair("userName",txtusername.getText().toString() ));
    nameValuePairs.add(new BasicNameValuePair("password",txtpassword.getText().toString() ));

    getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    client.execute(getMethod);

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

and server side:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        String mode=request.getParameter("Mode");
        if ("Login".equals(mode)) {
            String userName= request.getParameter("userName");
            String password= request.getParameter("password");
            System.out.println("post method: "+userName + "! pass :"+password);
            user = new User(password,userName);
            if (UserDao.authenticate(user)==true)
                out.write("Accept".toString());
            else
                out.write("Wrong".toString());
        } else if ("Register".equals(mode)) {
            String userName= request.getParameter("userName");
            String password= request.getParameter("password");
            String name= request.getParameter("name");
            String email= request.getParameter("email");
            String address= request.getParameter("address");
            String phoneNumber= request.getParameter("phoneNumber");
            System.out.println("reg mod: "+userName + "! pass :"+password);
            user = new User(userName,password,name,email,address, phoneNumber);

            if (UserDao.addUser(user) == true)
                out.write("Added");
            else
                out.write("NotAdded");
        }
    } catch (Exception ex) {
        System.out.println("Problem in message reading");
    }
}

1 Answer 1

1

Change the method name from processRequest() to doPost() and check your mapping in web.xml.May be the Servlet and URL mapping is not correct.Send your web.xml and the name of Servlet.

Sign up to request clarification or add additional context in comments.

2 Comments

At the begining: i write this:@WebServlet(name = "Login", urlPatterns = {"/Login"}) I called processRequest in doPost method: @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
then there might be a problem in your mapping.post your web.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.