7

I have imported the following 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.

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

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Jo.
  • 187
  • 2
  • 5
  • 9
  • This looks like a struts question rather than a servlet one - you might want to add a tag... – Jim Downing Nov 04 '09 at 14:53
  • Actually, what seems to be happening here is that the OP is trying to use the Struts2 API within a Java applet (which is pointless) – Dónal Nov 04 '09 at 15:07
  • This piece of code turns out to be inside an applet. This ain't going to work. If you elaborate more about the functional requirement for which you think that this is the solution, we may give a better suited solution. – BalusC Nov 04 '09 at 15:10

6 Answers6

9

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 1
    Thanks for your reply this is a legacy web application using asp.net 1.1 I need to generate a jar executable file i have tried to use request.getLocale() however i need to declare "request" somewhere no? So I guess i need to do HttpServletRequest request=??? string culture=request.getLocale().toString() and then pass the culture to my .net webservice call I am using IntelliJ Idea 8.1.3 Never used before in my life but is giving me these errors when trying to compile. Can you help? – Jo. Nov 04 '09 at 15:15
  • @Jo - The `HttpServletRequest` is purely server-side Java. If you do not have a Java web server, forget about it. It sounds like you need to open a request to a HTTP server. The `Accept-Language` header HTTP can be used, or the information can be sent in the body. How is that going to be done? Using `HttpURLConnection`? – McDowell Nov 04 '09 at 15:26
  • extremely grateful for the help you giving me guys. I am simply lost!!!Again i am not java dev All i need is an equivalent in java of string language=HttpContext.Current.Request.UserLanguages[0] is this the equivalent? Locale userLocale = Locale.getDefault(); will it work in a javaApplet? – Jo. Nov 04 '09 at 15:35
  • @Jo You have two problems: 1) how to get the user's Locale in the applet: use `Locale.getDefault()` as this will be the locale of the Java runtime in the browser; 2) how to send that information as part of your HTTP request to your .Net server: depends on how the request is being made. Presumably, the bug you're fixing is how to add user locale information to the HTTP request and the applet is already calling the web service. – McDowell Nov 04 '09 at 16:02
  • The bug I am fixing is that I need to localise something and I need the locale of the of the Java applet runtime in the browser. and pass it to the .net webservice. I have added the code Locale.getDefault() compiled into a jar file but the value i get in the webservice from the javaApplet is nothing. How can I test the locale.getDefault() is actually getting some value back eg "en-GB" for instance. How do you test a method on a javaapplet? thanks – Jo. Nov 04 '09 at 16:24
  • @Jo You should be able to print out the value to the Java Console. In Firefox, you can open the console via `Tools > Java Console` or the taskbar icon. Use `System.out.println(Locale.getDefault());` to print the locale. – McDowell Nov 04 '09 at 17:02
3

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.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
uncle bob
  • 570
  • 1
  • 10
  • 21
1

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

  1. Change your Struts action to implement the ServletRequestAware interface - this is the preferred method
  2. 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?

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 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(); Once I get the locale I pass it to the webservice (.net 1.1) I have added the code as mentioned in the post and I get cannot resolve HttpServletRequest any suggestions? Please note that I do know nothing about java – Jo. Nov 04 '09 at 15:00
  • @Jo - do not introduce Servlet dependencies in an applet. `request.getLocale()` would be of use only in a Java web server. – McDowell Nov 04 '09 at 15:03
  • Don Because I am in a webservice called by this JavaApplet doing HttpContext.Current.Request.UserLanguages[0] inside a webservice does not work.so it must come from the javaApplet. Doing HttpContext.Current.Request.UserLanguages[0] in a asp.net page it works just fine. – Jo. Nov 04 '09 at 15:58
1

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
Tim Hennekey
  • 2,186
  • 1
  • 13
  • 12
0

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.

neel4soft
  • 507
  • 1
  • 4
  • 12
0

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..