Java HttpServletRequest 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1674224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:02:42  来源:igfitidea点击:

HttpServletRequest cannot be resolved

javaservlets

提问by Jo.

I have imported the following import javax.servlet.http.*;

我已经导入了以下 import javax.servlet.http.*;

I want to get the preferred language Browser

我想获得首选语言浏览器

HttpServletRequest request = ServletActionContext.getRequest();
Locale userPreferredLocale = request.getLocale();

I get an error HttpServletRequest cannot be resolved.

我收到错误 HttpServletRequest 无法解析。

Can somebody help me and give me a step by step instruction if possible. I am not a java developer but a .net one and just fixing a bug.

如果可能的话,有人可以帮助我并给我一步一步的指导。我不是 Java 开发人员,而是 .net 开发人员,只是修复了一个错误。

thanks a lot

多谢

采纳答案by McDowell

The javax.servlet.http package is part of the servlet API. The relevant jars can be found in Java EE containers (such as Sun's Glassfish) or stand-alone servlet containers (like Apache's Tomcat). Essentially, these are Java web servers.

javax.servlet.http 包是 servlet API 的一部分。相关的 jar 可以在 Java EE 容器(如 Sun 的 Glassfish)或独立的 servlet 容器(如 Apache 的 Tomcat)中找到。本质上,这些是 Java Web 服务器。

In order to compile code that depends on it, you will have to add the servlet library to your dependencies. Exactly how that is done depends on the tools you are using.

为了编译依赖于它的代码,您必须将 servlet 库添加到您的依赖项中。具体如何完成取决于您使用的工具。

Are you building a web application? (Is the expected output a .war or .ear file?) Does the source come bundled with a build.xml(probably an Antbuild), any pom.xmlfiles (probably a Mavenbuild) or any .project/.classpathfiles (probably an Eclipseproject)?

您正在构建 Web 应用程序吗?(预期的输出是 .war 文件还是 .ear 文件?)源是否与build.xml(可能是Ant构建)、任何pom.xml文件(可能是Maven构建)或任何.project/.classpath文件(可能是Eclipse项目)捆绑在一起?



The scenario is this. Asp.net 1.1 having a javaapplet on a page calling a webservice. Javaapplet should detect the user preferred language in .net you do HttpContext.Current.Request.UserLanguages[0] so i asked and apparently in java the equivalent is request.getLocale();

场景是这样的。Asp.net 1.1 在调用 web 服务的页面上有一个 javaapplet。Javaapplet 应该检测 .net 中的用户首选语言,您执行 HttpContext.Current.Request.UserLanguages[0] 所以我问,显然在 Java 中等效的是 request.getLocale();

OK, ignore what I said above. To get the Localein an Applet, I imagine you would just use:

好吧,忽略我上面说的。要在 Applet 中获取语言环境,我想您只需使用:

Locale userLocale = Locale.getDefault();

On a Java web server, you would use request.getLocale()to pick up the user's locale from the HTTP request. Applets run on the client.

在 Java Web 服务器上,您将使用request.getLocale()从 HTTP 请求中获取用户的语言环境。小程序在客户端上运行。

回答by Dónal

It looks like you're using Struts2. There are two ways to access the HttpServletRequest object

看起来您正在使用 Struts2。访问HttpServletRequest对象有两种方式

  1. Change your Struts action to implement the ServletRequestAwareinterface - this is the preferred method
  2. The method you have shown above
  1. 更改您的 Struts 操作以实现ServletRequestAware接口 - 这是首选方法
  2. 你上面显示的方法

Given that (1) is the preferred method, I suggest you try this instead, more details here.

鉴于 (1) 是首选方法,我建议您改用此方法,更多详情请点击此处

Update:Based on the comment you've added it seems like you're not actually using Struts. You're using .Net on the server side and a Java Applet on the client-side. If that is the case, there's no point in trying to use the Servlet or Struts2 APIs, as they are server-side only

更新:根据您添加的评论,您似乎并未实际使用 Struts。您在服务器端使用 .Net,在客户端使用 Java Applet。如果是这种情况,那么尝试使用 Servlet 或 Struts2 API 就没有意义了,因为它们只是服务器端的

Given that you already know how to get the user's preferred language on the .Net server-side, I don't understand why you don't just do that?

鉴于您已经知道如何在 .Net 服务器端获取用户的首选语言,我不明白您为什么不这样做?

回答by Tim Hennekey

When you compile the source you will need to add the jar containing the servlet class to the classpath. One way to do this is with the -cp flag:

编译源代码时,您需要将包含 servlet 类的 jar 添加到类路径中。一种方法是使用 -cp 标志:

javac -classpath lib/servlet.jar MyClass.java

回答by BalusC

To get the Locale inside an Applet, use the inherited getLocale()method.

要获取 Applet 中的 Locale,请使用继承的getLocale()方法。

Example:

例子:

public class MyApplet extends Applet {
    public void doStuff() {
        Locale locale = getLocale();
        // Now you can work with that.
    }
}

回答by neel4soft

javax.servlet.http and all classes related servlet context and servlet programming is related to your Servlet Container only. So stop worrient about anything else and check if Tomcat libraries are being included in your WEB-APP class path.

javax.servlet.http 和所有与 servlet 上下文和 servlet 编程相关的类仅与您的 Servlet 容器相关。所以不要担心其他任何事情,并检查 Tomcat 库是否包含在您的 WEB-APP 类路径中。

If not add them and everything will be fine.

如果不添加它们,一切都会好起来的。

Right Click on your project > Properties > Add Libraries > Server Runtime

右键单击您的项目 > 属性 > 添加库 > 服务器运行时

and choose your server that is associated with your application.

并选择与您的应用程序关联的服务器。

You are done, this will include Servlet Container libraries to your project and HttpServletRequest & HttpServletResponse classes will be resolved.

大功告成,这将包括到您的项目的 Servlet 容器库,并且 HttpServletRequest 和 HttpServletResponse 类将被解析。

Hope it helps, more information about Servlet Architecture and context can be found Here.

希望它有所帮助,可以在此处找到有关 Servlet 体系结构和上下文的更多信息

回答by adigayo

I've got this message "The import javax.servlet.http.HttpSession cannot be resolved" when deploying my web app to Glassfish 4.0 Server (on Eclipse IDE). Then I tried these steps :

将我的 Web 应用程序部署到 Glassfish 4.0 服务器(在 Eclipse IDE 上)时,我收到此消息“无法解析导入 javax.servlet.http.HttpSession”。然后我尝试了这些步骤:

  • right-click project > properties > Project Facets > Glassfish Web Extention > Runtime > checklist GlassFish Instance (eg : GlassFish 4.0 at Localhost)
  • click Apply
  • 右键单击项目 > 属性 > 项目构面 > Glassfish Web 扩展 > 运行时 > 清单 GlassFish 实例(例如:GlassFish 4.0 at Localhost)
  • 单击应用

It works fine..

它工作正常..

回答by uncle bob

You can do the following: import the jar file inside you class:

您可以执行以下操作:在您的类中导入 jar 文件:

import javax.servlet.http.HttpServletResponse

import javax.servlet.http.HttpServletResponse

add the Apache Tomcat library as follow:

添加Apache Tomcat库如下:

Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 6.0 > Finish > Ok

项目 > 属性 > Java 构建路径 > 库 > 从库选项卡添加库 > 选择服务器运行时 > 下一步 > 选择 Apache Tomcat v 6.0 > 完成 > 确定

Also First of all, make sure that Servlet jar is included in your class path in eclipse as PermGenErrorsaid.

另外首先,确保 Servlet jar 包含在 eclipse 中的类路径中,如上所述PermGenError