Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest cannot be resolved

Tags:

java

servlets

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

like image 463
Jo. Avatar asked Nov 04 '09 14:11

Jo.


People also ask

How do you fix Httpservlet Cannot be resolved to a type?

The reason is the Java Servlet API is missing in the project's classpath. You can solve this problem by specifying a server runtime for the project, e.g. Apache Tomcat runtime – because a Java web server is a servlet container that implements the Servlet API.

What is javax servlet http package?

The javax. servlet. http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.


2 Answers

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.

like image 51
McDowell Avatar answered Oct 04 '22 04:10

McDowell


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.

like image 32
uncle bob Avatar answered Oct 04 '22 03:10

uncle bob