简体   繁体   中英

Android using java servlets from an existing web application

I have an existing and working java web application. I want to make this application native on android and intend to keep using the same servlets.

The last time I was doing an android application that communicated with the server was placing something like this in the servlet:

PrintWriter out = response.getWriter(); 
out.print("pass"); 

and in the android java class I would do this:

String response = null;
    try {
        response = CustomHttpClient.executeHttpPost(
        "http://10.0.2.2/ProjectName/" + "LoginServlet",postParameters);
        String res = response.toString();
                    if(res.equals("pass")
                    {
                        /**do the things i want**/
                    }
               }
               catch(Exception e)
               {
                   e.printStackTrace();
               }

The servlets that are in my web application uses HttpSession and has a dispatch method like this:

HttpSession session = request.getSession();
session.setAttribute("user",user);

and this:

dispatch(request, response, "/login.jsp");

I added the PrintWriter to the existing servlets which my web application uses it failed. And this error was produced on logcat:

07-12 08:59:46.344: E/jdwp(487): >>> comparing 'jsp' to 'ers'
07-12 08:59:46.454: E/jdwp(487): >>> comparing 'jsp' to 'nfo'
07-12 08:59:46.524: E/jdwp(487): >>> comparing 'jsp' to 'o$1'
07-12 08:59:46.534: E/jdwp(487): >>> comparing 'jsp' to 'nfo'
07-12 08:59:46.534: E/jdwp(487): >>> comparing 'jsp' to 'o$1'
07-12 08:59:46.594: E/jdwp(487): >>> comparing 'jsp' to 'Key'
07-12 08:59:46.934: E/jdwp(487): >>> comparing 'jsp' to 'ver'
07-12 08:59:47.044: E/jdwp(487): >>> comparing 'jsp' to 'ile'
07-12 08:59:47.334: E/jdwp(487): >>> comparing 'jsp' to 'gin'
07-12 08:59:47.334: E/jdwp(487): >>> comparing 'jsp' to 'nfo'
07-12 08:59:47.354: E/jdwp(487): >>> comparing 'jsp' to 'ame'  
07-12 08:59:47.404: E/jdwp(487): >>> comparing 'jsp' to 'ger'
07-12 08:59:47.404: E/jdwp(487): >>> comparing 'jsp' to 'eme'
07-12 08:59:47.504: E/jdwp(487): >>> comparing 'jsp' to 'ams'

and the list of comparisons go on.

Question: Am I missing something or I should just create new servlets? (I really hope its not the second option)

Edit

I've found out that the error is produced by the dispatch method which changes the response to a jsp file instead of just a text string, and commenting out the dispatch method allows me to login to server successfully using android.

However, I want to keep the dispatch method AND the PrintWriter such that both android application and web application can use the same single servlet to login. Anyway I can do that?

After some research, the answer to this question seems to be a no. There isn't a way to use the same servlet for the web application and the android application. A new servlet has to be created, no matter if the 2 servlets are 95% the same in content.

There are few ways that I currently know which the login with android can be done:

1) using the out.print method as mentioned above. Create PrintWriter object and use the object to do out.print, and check it by response.toString().equals("expectedString") in the android java class.

2) cookies, which I thought could have worked but no, the dispatch method still got in the way. I'm still quite vague when it comes to cookies but apparently you can keep user information in the cookies so the user will be authenticated throughout the use of the application.

Feel free to add to the answer or correct me if I'm wrong in any of the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM