tags:

views:

1468

answers:

4

Hi all 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

A: 

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?

Don
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
@Jo - do not introduce Servlet dependencies in an applet. `request.getLocale()` would be of use only in a Java web server.
McDowell
DonBecause 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
+3  A: 

The javax.servlet.http package is part of the servlet API. The relevant jars can be found in JEE 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.

McDowell
+1: he may actually need just the Locale.
BalusC
Thanks for your replythis is a legacy web application using asp.net 1.1 I need to generate a jar executable filei 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 callI am using IntelliJ Idea 8.1.3Never used before in my life but is giving me these errors when trying to compile.Can you help?
Jo
@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
extremely grateful for the help you giving me guys.I am simply lost!!!Again i am not java devAll i need is an equivalent in java ofstring language=HttpContext.Current.Request.UserLanguages[0]is this the equivalent? Locale userLocale = Locale.getDefault();will it work in a javaApplet?
Jo
@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
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
@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
A: 

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
There's no point adding the servlet API to a Java applet
Don
A: 

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

Example:

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