HttpServletRequest could not be resolved

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

I want to get my preferred browser language

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

I get an error message. HttpServletRequest could not be resolved.

Can someone help me and give step-by-step instructions if possible. I am not a Java developer, but one .net and fix the error.

Many thanks

+6
java servlets
source share
7 answers

The javax.servlet.http package is part of the servlet API. Relevant jars can be found in Java EE containers (e.g. Sun Glassfish) or stand-alone servlet containers (e.g. Apache Tomcat). Essentially, these are Java web servers.

To compile code that depends on it, you will need to add a servlet library to your dependencies. Exactly how this is done depends on the tools you use.

Are you building a web application? (Is the expected output a .war or .ear file?) The source comes bundled with build.xml (possibly Ant build), any pom.xml files (possibly Maven build), or any .project/.classpath (maybe Eclipse project)?


The scenario is as follows. Asp.net 1.1 with javaapplet on the page calling the web service. The Javaapplet should determine the preferred user language in .net, you do HttpContext.Current.Request.UserLanguages โ€‹โ€‹[0], so I asked and apparently in java the equivalent of request.getLocale ();

OK, ignore the above. To get Locale in an applet, I assume that you just use:

 Locale userLocale = Locale.getDefault(); 

On the Java web server, you should use request.getLocale() to get the user locale from the HTTP request. Applets run on the client.

+9
source

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

  • Change the Struts action to implement the ServletRequestAware interface - this is the preferred method
  • The method that you showed above

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

Update: Based on the added comment, it looks like you are not actually using Struts. You are using .Net on the server side and a Java applet on the client side. If so, it makes no sense to try using the Servlet or Struts2 APIs, since they are only server-side

Given that you already know how to get your preferred user language on the .Net side, I donโ€™t understand why you are not just doing this?

+1
source

When compiling the source, you 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 
0
source

To get the locale inside the applet, use the inherited getLocale () method.

Example:

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

javax.servlet.http and all classes related to servlet context and servlet programming are only associated with your servlet container. So stop worrying about anything else and see if Tomcat libraries are included in your path to the WEB-APP class.

If you do not add them, everything will be fine.

Right-click on your project> Properties> Add Libraries> Server Runtime

and select your server associated with your application.

You are done, this will include the Container servlet libraries in your project, and the HttpServletRequest and HttpServletResponse classes will be enabled.

Hope this helps, more details on the Servlet architecture and context can be found here.

0
source

I have this message "Import javax.servlet.http.HttpSession cannot be resolved" when deploying my web application to Glassfish 4.0 server (in Eclipse IDE). Then I went through the following steps:

  • right-click project> properties> project grants> Glassfish web extension> runtime> GlassFish Instance checklist (e.g. GlassFish 4.0 on localhost)
  • click "apply"

It works great.

0
source

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

import javax.servlet.http.HttpServletResponse

add the Apache Tomcat library as follows:

Project> Properties> Java Build Path> Libraries> Add Library from Library Tab> Select Server Runtime> Next> select Apache Tomcat v 6.0> Finish> Ok

Also, first of all, make sure that the Servlet key is included in eclipse when eclipse starts, like PermGenError .

0
source

All Articles