简体   繁体   English

Android使用现有Web应用程序中的Java Servlet

[英]Android using java servlets from an existing web application

I have an existing and working java web application. 我有一个现有的并且正在运行的Java Web应用程序。 I want to make this application native on android and intend to keep using the same servlets. 我想使此应用程序在android上原生,并打算继续使用相同的servlet。

The last time I was doing an android application that communicated with the server was placing something like this in the servlet: 我上一次做与服务器通信的android应用程序时,将这样的内容放置在servlet中:

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

and in the android java class I would do this: 在android java类中,我会这样做:

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: 我的Web应用程序中的servlet使用HttpSession并具有如下的调度方法:

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. 我将PrintWriter添加到了我的Web应用程序使用的现有servlet失败。 And this error was produced on logcat: 并且此错误是在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? 问题:我是否缺少某些内容,还是应该只创建新的servlet? (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. 我发现错误是由调度方法产生的,该方法将响应更改为jsp文件而不是文本字符串,并且注释掉调度方法使我可以使用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. 但是,我想保留调度方法和PrintWriter,以便android应用程序和web应用程序都可以使用相同的单个servlet登录。 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. 无法为Web应用程序和android应用程序使用相同的servlet。 A new servlet has to be created, no matter if the 2 servlets are 95% the same in content. 无论两个servlet的内容是否95%相同,都必须创建一个新的servlet。

There are few ways that I currently know which the login with android can be done: 我目前知道几种可以通过android登录的方法:

1) using the out.print method as mentioned above. 1)使用上述out.print方法。 Create PrintWriter object and use the object to do out.print, and check it by response.toString().equals("expectedString") in the android java class. 创建PrintWriter对象,并使用该对象执行out.print,并通过android java类中的response.toString()。equals(“ expectedString”)进行检查。

2) cookies, which I thought could have worked but no, the dispatch method still got in the way. 2)cookie,我以为可以用,但是没有,派遣方法仍然受阻。 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. 关于cookie,我仍然很模糊,但是显然您可以将用户信息保留在cookie中,以便在整个应用程序使用过程中对用户进行身份验证。

Feel free to add to the answer or correct me if I'm wrong in any of the above. 如果以上任何内容有误,请随时添加答案或纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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