HttpServletRequest cannot be resolved
I have imported the following import javax.servlet.http.*;
I want to get the preferred language Browser
HttpServletRequest request = ServletActionContext.getR开发者_JS百科equest();
Locale userPreferredLocale = request.getLocale();
I get an error HttpServletRequest cannot be resolved.
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.
thanks a lot
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.
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.
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 Ant build), any pom.xml
files (probably a Maven build) or any .project/.classpath
files (probably an Eclipse project)?
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();
OK, ignore what I said above. To get the Locale in an Applet, I imagine you would just use:
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.
You can do the following: import the jar file inside you class:
import javax.servlet.http.HttpServletResponse
add the Apache Tomcat library as follow:
Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 6.0 > Finish > Ok
Also First of all, make sure that Servlet jar is included in your class path in eclipse as PermGenError
said.
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:
javac -classpath lib/servlet.jar MyClass.java
It looks like you're using Struts2. There are two ways to access the HttpServletRequest object
- Change your Struts action to implement the ServletRequestAware interface - this is the preferred method
- The method you have shown above
Given that (1) is the preferred method, I suggest you try this instead, more details here.
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
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?
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.
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.
Hope it helps, more information about Servlet Architecture and context can be found Here.
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 :
- right-click project > properties > Project Facets > Glassfish Web Extention > Runtime > checklist GlassFish Instance (eg : GlassFish 4.0 at Localhost)
- click Apply
It works fine..
精彩评论