Compile java servlet
When compiling the servlet below I get the errors below:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)throws Servlet
Exception, IOException
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<Hello Readers>");
out.println("</html>");
out.println("</body>");
Why don't these packages exist?
Thanks!
/usr/java/j2sdk1.4.0/bin/javac HelloServlet.java
HelloServlet.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
HelloServlet.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloServlet.java:5: cannot resolve symbol
symbol : class HttpServlet
location: class HelloServlet
public class HelloServlet extends HttpServlet
^
HelloServlet.java:7: cannot resolve symbol
symbol : class HttpServletRequest
location: class HelloServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)throws Servlet
Exception, IOException
^
HelloServlet.java:7: cannot resolve symbol
symbol : class HttpServletResponse
location: class HelloServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)throws Servlet
Exception, IOException
HelloServlet.java:7: cannot resolve symbol
symbol : class ServletException
location: class HelloServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)throws Servlet
Exception, IOException
^
6 errors
Those packages do exists.
You just need to especify the JAR file where they are located in your classpath when compiling.
Those packages are not part of the Java 2 Standard Edition (J2SE). Download the Enterprise Edition SDK (J2EE SDK) and use the servlet.jar when compiling.
Similar Messages
-
Compiling Java Servlet with Depricated Methods
I have a problem of compiling Java servlets that contain depricated methods. If you compile against modern version of servlet.jar I see depricated methods error messages and if I use a very old servlet.jar it cannot find newer methods. What is is the best solution in this case?
Thank you,
Boris.The best thing to do would be compile your code with the -depreciated option to find which methods have been depreciated and the refactor your code to remove them.
The next best thing is to ignore the messages (they are warnings not errors) and your code should still work -
Compiling java servlet programs
i have Tomcat 5.0 and SDK 1.5 . I am able to compile java programs. But in servlet programs the compiler is unable to recognize the javax.servlet package(the code below). I have pushed all the JAR files of TOMCAT to the CLASSPATH . But nothing has changed.
javax.servlet.*;
javax.servlet.Http.*;hi,..
javax.servlet.Http.*; should be javax.servlet.http.*; !
even though you have put the jar files in the classpath .. does it point to servlet-api.jar ? if it does.. it should work. Also try out specifing the classpath using the -cp while compiling the servlet program .
--Olakara -
this the error i encounter.. i have read some of the posted topics on it but i dont understand how i can create my own classpath..where should i do that..what should i write...how i have to save it...pls help me...
C:\currentyaazmin1\java>javac login2.java
login2.java:9: Package javax.servlet.http not found in import.
import javax.servlet.http.*;
^
login2.java:11: Package javax.servlet not found in import.
import javax.servlet.*;
^
login2.java:16: Superclass HttpServlet of class login2 not found.
public class login2 extends HttpServlet
^
3 errors
i have already installed jdk1.3 an jsdk2.1...i also copied javax folder from jsdk2.1 and pasted in c:....still i encouter the same thing... pls helpDownload j2ee is recommended to solve any compling such as servlets and swing
http://java.sun.com/j2ee/download.html
Once done all you do is include j2ee.jar and you have xml, servlets, rmi the works!! :)
However since you download the servelet component only, search in you hard drive for ".jar" file.
You can open the .jar file with a WinZip and you can check if it as javax.servlet.http.* by looking at the far right for javax/servlet/http
Once you know the jar file to include. Next is to put it in the classpath. The most simple way using NT example, how Windows 95/98 is similar. Go to:
Control Panel -> Double Click on System -> Click on Environment tab
Type below in Variable: CLASSPATH
Type in Value: PATH TO SERVLET;.;
Don't forget the ;.; (the dot for currnet path to run)
If you need to add more to the classpath, seperate by classpath.
Log off and log back in as your user. No need to reset the machine.
The system will pick the new CLASSPATH.
Test on dos as "echo %CLASSPATH%" to see what its set to. -
Compile java applications,servlets etc.
I am a newbie to java...what do i need to compile java applications,servlets etc.
first you need java developers kit(jdk)
download it
then r.click on my computer and take it's properties
go to advance properties
go to envirinment variable
set variables
JAVA_HOME set to directory where you installed jdk
CLASS_PATH set to the bin in your jdk
then write a java class and save somewhere as .java file
go to command prompt
change directory to where you save java class
and use javac (yourjavafilename) .java command to compile it
wish you all the best -
How to compile and run a Java servlet using the BEA weblogic server
Hi,
Could you help me out as to how to compile a servlet using Web Logic server.I have written a Servlet program and have also set up the environment in the "c:\bea\wlserver6.0\config\examples" folder by giving the command setExamplesEnv.cmd.
I have stored my GreetingServlet.java file in "c:\bea\wlserver6.0\samples\examples\servlets" folder.
How do I compile my servlet now?
Vid
Hello,
1. The .form file was used by Netbean's GUI builder to create the .java and is not needed for compilation.
2..../src # javac -classpath "..." mytool/*.java
I guess the package mytool was not coped with.
Alternatively it is worth to use the ant build tool also used by Netbeans.
Then you can make a jar from the class files, indicate in the manifest.mf file:
1. the main class
2. the library jars -
I have written a little servlet program. When it is compiled, I get errors saying "javax.servlet does not exist".
I have J2EE SDK 1.4 installed. Does the error mean the classpath is missing? Appreciate advise.Hi !
I had same error yesterday. But later I was able to compile the servlet by setting CLASSPATH.
Set CLASSPATH as follows if you have installed J2EE AppServer in c:\
1. Locate j2ee.jar in the directory you installed J2EE AppServer which can be in:
C:\Sun\AppServer\lib\j2ee.jar
2. Go to My Computer properties
3. Click on the Environmental variable button of Advanced tab
4. If you already have CLASSPATH variable then click on Edit button else click on New to set the CLASSPATH as follows:
CLASSPATH=C:\Sun\AppServer\lib\j2ee.jar
I think this should work for you.
Goodluck,
Anurag -
Tomcat 6 Calling a Java Servlet from a JSP Page
Below is a very simple JSP Page that calls a Java Servlet. The question is given Tomcat security constraints, is it possible to call a servlet from a JSP and get the correct output without getting an error message? If so, how would you code the web.xml file?
c:\apache-tomcat-6.0.18
Under conf
catalina
localhost
HelloWorldExample.xml is directly under localhost
The application would have this directory structure:
webapps
HelloWorldExample
hello.jsp is directly under HelloWorldExample
Under HelloWorldExample
src
WEB-INF
classes
Under classes
jservlets
HelloWorld.java is in src folder
HelloWorld.class is in jservlets folder
HelloWorldExample.xml
<Context path="/HelloWorldExample" docBase="HelloWorldExample" debug="0"
reloadable="true" crossContext="true">
</Context>**************************
hello.jsp
<HTML>
<HEAD>
<TITLE>Hello</TITLE>
</HEAD>
<BODY>
<FONT SIZE="4">
<P>
Please enter your name:
<FORM
METHOD="Post"
ACTION="servlet/jservlets.HelloWorld">
<TABLE BORDER="3" CELLPADDING="1" WIDTH="100%" ALIGN="CENTER">
<TR>
<TD><B>Name:</B></TD>
<TD><INPUT TYPE="text" NAME="Name" VALUE="" SIZE="65"> </TD>
</TR>
</TABLE>
<P>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</FONT>
</BODY>
</HTML>******************
HelloWorld.java
package jservlets;
import java.io.*;
import java.util.Date;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
PrintWriter out;
PrintWriter err;
String strName;
public void displayMessage(HttpServletRequest request, HttpServletResponse response)
throws Exception
try
if (!strName.equals("") && strName != null)
out.println("Hello " + strName + "" + "<P>");
out.println("Hello World" + "<P>");
else
out.println("Hello World" + "<P>");
catch (Exception e)
out.println("Exception: Could not display message." + "<P>");
err.println (e.getMessage () ) ;
out.println("<P>");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
try
response.setContentType("text/html");
out = response.getWriter();
err = response.getWriter();
strName = request.getParameter("Name").trim();
out.println("<html><head><title>");
out.println("</title></head><body>");
out.println("<FORM");
out.println("METHOD=POST");
out.println("ACTION=http://localhost:8080/HelloWorldExample/hello.jsp>");
out.println("<TABLE ALIGN='RIGHT'>");
out.println("<TR>");
out.println("<TD>");
out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Hello World Page\";>");
out.println("</INPUT>");
out.println("</TD>");
out.println("</TR>");
out.println("</TABLE>");
out.println("</FORM>");
out.println("<BR CLEAR='all'>");
out.println("<P>");
displayMessage(request, response);
out.close();
out.println("</body></html>");
catch(Throwable e)
e.printStackTrace();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
web.xml
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>jservlets.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping> ******************************
HelloWorld.java can be compiled by using javac.
Once compiled, HelloWorld.class would be moved to the jservlets folder.
FYI, coding the above url-pattern results in:
HTTP Status 404
The requested resource (/HelloWorldExample/servlet/jservlets.HelloWorld) is not available
The following url-pattern in the web.xml file permits the servlet to be executed but results in a null pointer exception:
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/ </url-pattern>
</servlet-mapping> **************************************************
RobinThis problem was resolved.
In hello.jsp
ACTION="servlet/jservlets.HelloWorld">
was replaced with
ACTION="servlet/HelloWorld">
Robin -
Using a Compiled Java Class --- Error
Hi All,
I am new to jsp tech. I am using 'Sun Java system application web server'.
I am getting following error when running ch06_02.jsp :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
Generated servlet error:
[javac] /nfs/bxslnx/home/bxs/install/SUNWappserver/domains/domain1/generated/jsp/j2ee-modules/__default-web-module-server/org/apache/jsp/jsp/Examples/ch06/ch06_005f02_jsp.java:6: '.' expected
[javac] import ch06_01;
[javac] ^
[javac] 1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:88)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:337)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:429)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:489)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:516)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:307)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)
javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
note The full stack trace of the root cause is available in the Sun-Java-System/Application-Server logs.
My two files are :
ch06_01.java
public class ch06_01
public ch06_01()
public String msg()
return "Hello from JSP!";
ch06_02.jsp
<%@page import="ch06_01" %>
<HTML>
<HEAD>
<TITLE>Using a JavaBean</TITLE>
</HEAD>
<BODY>
<H1>Using a JavaBean</H1>
<% ch06_01 messager = new ch06_01(); %>
The message is: <%= messager.msg() %>
</BODY>
</HTML>
Please help me ....... I don't know whats going wrong . I m using Suse OS.
Thanks
BasantHi
You have to place the xml parsing library inside the lib directory under the WTK.
Mihai -
Compiling .java file on tomcat 4.1
i wana know where to store the .java file on the tomcat directory for tomcat to compile it?
and then after putting it in the appropriate place is this command gona compile it: javac -cp "c:program files\apache group\tomcat 4.1\common\lib\jasper-compiler.jar" yourservlet.java
if this is not the correct command then wat is the easiest way to compile the servlet?
thank you in advance.Hi,
I've never heard about .java auto compilation in Tomcat, but anyway, that wouldn't be a goddo aproach, don't you think so? Why don't you publish your application in a single .war file? You could even sent you JSPs compiles, avoiding performance problems... -
Problem Installing Java Servlets Package
I downloaded a sample servlet program which imports classes from the Java Servlets Package. I have the JDK 1.2 which does not come with the servlet classes (javax.servlet and javax.servlet.http). So I then downloaded them from java.sun.com. They come as a set of class files in .zip format. I thought packages were supposed to be in .jar format? At any rate, my compiler still fails to see these classes even after I have extracted these files into the same directory as the original .zip file and added this directory to my classpath. Am I going about this the wrong way? Any ideas on how I can make my compiler see these classes? --Thanks
I down loaded j2sdk 1.4 . and also down loade servlet package. I installed in bin directory.
i took the sample program and compiled. there is error message " java servlet package not found.
please help. email me at [email protected]
Thanks
sri -
Problems calling Java Servlets from HTML pages Online
Hello
I have created a Web site using Java Servlets, and have acquired some servlet enabled web-space however i am having some difficulty in calling the actual servlets from the HTML pages i was using the line of code as follows
http://localhost:8080/servlet/....
followed by the name eg.
http://localhost:8080/servlet/Login
however this doesn't seem to be working i have also tried using the exact address of the servlet but this didn't work either
i.e ..servlet/Login.java
I was wondering would anyone have any idea as in how the servlets should be called
Thanks very muchOnce you write the Servlet code, you have to compile and put the classes in the server classpath. To refer these servlets from your pages, you have to configure them in the server configuration(typical a xml file). There you define how you are going to refer to the servlet(/servlet/Logon) and the correponding class.
-Mak -
Unicode, UTF-8 and java servlet woes
Hi,
I'm writing a content management system for a website about russian music.
One problem I'm having is trying to get a java servlet to talk Unicode to the Content mangament client.
The client makes a request for a band, the server then sends the XML to the client.
The XML reading works fine and the client displays the unicode fine from an XML file read locally (so the XMLReader class works fine).
The servlet unmarshals the request perfectly (its just a filename).
I then find the correct class, and pass it through the XML writer. that returns the XML as string, that I simply put into the output stream.
out.write(XMLWrite(selectedBand));I have set correct header property
response.setContentType("text/xml; charset=UTF-8");And to read it I
//Make our URL
URL url = new URL(pageURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true); // want to send
conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" );
conn.setRequestProperty( "Content-length", Integer.toString(request.length()));
conn.setRequestProperty("Content-Language", "en-US");
//Add our paramaters
OutputStream ost = conn.getOutputStream();
PrintWriter pw = new PrintWriter(ost);
pw.print("myRequest=" + URLEncoder.encode(request, "UTF-8")); // here we "send" our body!
pw.flush();
pw.close();
//Get the input stream
InputStream ois = conn.getInputStream();
InputStreamReader read = new InputStreamReader(ois);
//Read
int i;
String s="";
Log.Debug("XMLServerConnection", "Responce follows:");
while((i = read.read()) != -1 ){
System.out.print((char)i);
s += (char)i;
return s;now when I print
read.getEncoding()It claims:
ISO8859_1Somethings wrong there, so if I force it to accept UTF-8:
InputStreamReader read = new InputStreamReader(ois,"UTF-8");It now claims its
UTF8However all of the data has lost its unicode, any unicode character is replaced with a question mark character! This happens even when I don't force the input stream to be UTF-8
More so if I view the page in my browser, it does the same thing.
I've had a look around and I can't see a solution to this. Have I set something up wrong?
I've set, "-encoding utf8" as a compiler flag, but I don't think this would affect it.I don't know what your problem is but I do have a couple of comments -
1) In conn.setRequestProperty( "Content-length", Integer.toString(request.length())); the length of your content is not request.length(). It is the length of th URL encoded data.
2) Why do you need to send URL encoded data? Why not just send the bytes.
3) If you send bytes then you can write straight to the OutputStream and you won't need to convert to characters to write to PrintWriter.
4) Since you are reading from the connection you need to setDoInput() to true.
5) You need to get the character encoding from the response so that you can specify the encoding in InputStreamReader read = new InputStreamReader(ois, characterEncoding);
6) Reading a single char at a time from an InputStream is very inefficient. -
Java Servlet Error in oracle 10.1.2 (10g)
Hello,
This question belongs to our company project,which is a Govt.Organization. We are using AIX 5.3 Server OS with Oracle 10.1.2 (10g) Application Server on it.
We have installed all the above OS & Oracle Application on IBM P-Series High End Servers.
Now, We have successfully installed "Oracle 10g AS" on AIX but after installation, we are getting some common errors which is related to JAVA & Servlet. Below is the error file.
09/05/02 19:01:50 Started
09/05/02 19:01:51 vatwebsite: jsp: init
09/05/02 19:01:51 vatwebsite: FrontServlet: init
09/05/02 19:01:52 vatwebsite: action: init
09/05/02 19:01:54 vatwebsite: barbecue: init
09/05/02 19:01:54 vatwebsite: Started
09/05/02 19:01:55 vatwebsite: Servlet error
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java(Compiled Code))
at java.util.HashMap$KeyIterator.next(HashMap.java(Compiled Code))
at java.util.Collections$6.nextElement(Collections.java:2493)
at org.apache.struts.util.RequestUtils.getModulePrefixes(RequestUtils.java:1907)
at org.apache.struts.util.RequestUtils.getModuleName(RequestUtils.java:1803)
at org.apache.struts.util.RequestUtils.getModuleName(RequestUtils.java:1788)
at org.apache.struts.util.RequestUtils.selectModule(RequestUtils.java:1768)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1481)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at com.vat.website.utils.LogRecorderServlet.doPost(LogRecorderServlet.java:38)
at com.vat.website.utils.LogRecorderServlet.doGet(LogRecorderServlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:824)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:330)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:830)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].server.http.AJPRequestHandler.run(AJPRequestHandler.java:224)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].server.http.AJPRequestHandler.run(AJPRequestHandler.java:133)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.2)].util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:186)
at java.lang.Thread.run(Thread.java:568)
I need some solution to remove this error. Kindly let me know how to go further to sort out this error. Another thing is,I am not a JAVA/Programmer person But still this is a part of my job.
So,please do the needful & provide me some guidence & document about "How to solve this JAVA Error".
Advanced Thanks,
Nishith Vyas.See this thread for a JDBC bug number reference
JDev 10.1.3 : Error running my first ADF application. -
Suffering an complie error when trying to compile java class in EBS11i
Hi,
When I trying to compile java classes with which imported the HttpServletResponse class, will get the follow error message:
package javax.servlet does not exist
cannot resolve symbol
symbol : class HttpServletResponse
It seems the javax.servlet package is not included in the classpath. But I checked the $CLASSPATH, it seems no problem.
echo $CLASSPATH
/u02/applvis/viscomn/util/java/1.4/j2sdk1.4.2_04/lib/tools.jar:/u02/applvis/viscomn/util/java/1.4/j2sdk1.4.2_04/lib/dt.jar:/u02/applvis/viscomn/util/java/1.4/j2sdk1.4.2_04/jre/lib/charsets.jar:/u02/applvis/viscomn/util/java/1.4/j2sdk1.4.2_04/jre/lib/rt.jar:/u02/applvis/viscomn/java/appsborg2.zip:/u02/applvis/visora/8.0.6/forms60/java:/u02/applvis/viscomn/java
Does anyone know the reason?
environment: ebs 11i
Thanks&Regards,
Xiaofengresolved this issue.
1. Edit $APPL_TOP/admin/adovars.env file -
Add the following jar files to the AF_CLASSPATH line -
Full path of /...../iAS/Apache/Jsdk/lib/jsdk.jar
Full path of /...../iAS/Apache/Jserv/libexec/ApacheJServ.jar
2. Bounce the concurrent manager in order to have the changes take effect.
Maybe you are looking for
-
How do I upgrade to the full version of creative cloud?
I see you have billed my credit card for a single app version, but I think I want to upgrade to the full version for $49.99/month. How do I do this? Also it seems to me I can only download the trial version of dreamweaver (I purchased the membership
-
Still not working properly for me. How about anyone else? It's an improvement - I see a few songs in them now, but in some I am only seeing one or two where there should be hundreds. Haven't been able to check the play count / last played yet but wil
-
Storing PL/SQL code in a table
Can PL/SQL procedure be stored in a table and used or executed in a VB application by selecting that column in the VB? Thanks
-
How can i take a picture of a website on my mac?
I would like to take a picture of my website. How can I do this?
-
After I shut down the & than restart the firefox icon does not reappear in my dock.