This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Head First Java - Servlet  RSS feed

 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to compile the following:



I am receiving this error in command prompt -

MyServletA.java:7: error: cannot find symbol
       public void doGet(HttpServerletRequest request, HttpServerletResponse response)
                         ^
 symbol:   class HttpServerletRequest
 location: class MyServletA
MyServletA.java:7: error: cannot find symbol
       public void doGet(HttpServerletRequest request, HttpServerletResponse response)
                                                       ^
 symbol:   class HttpServerletResponse
 location: class MyServletA
2 errors

I have checked my environment variables and they are:


CLASSPATH is
C:\Users\......\Downloads\java_ee_sdk-7u3\glassfish4\mq\lib\javax.servlet-api.jar


JAVA_PATH is

C:\Program Files\Java\jdk1.8.0_191\bin

Could someone please tell me how I can fix this?
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh dear thanks lol
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So a different issue now, I have created a webpage as per the book. This is the code:


<HTML>
<BODY>    
<a href=”servlets/MyServletA”>This is an amazing servlet.</a>  
</BODY>
</HTML>

Now when i open up the file and click on 'This is an amazing servlet' it just says cannot reach this page, yet the path is correct.

If i navigate to the path provided it works - it also seems to put the servlet folder and the java file like this:

/”servlets/MyServletAâ€

How do i fix this?
 
Carey Brown
Saloon Keeper
Posts: 5456
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Carey Brown
Saloon Keeper
Posts: 5456
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using a plain text editor to write your code, such as notepad or notepad++. What editor are you using?
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok I have changed that now it still does not work, however if i do put MyServletA.java then the link just prints out the java code but not just the message?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3241
283
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at this small tutorial, it is old but still usable: HowToCreateWebApplicationWithoutAnIDE
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frits Walraven wrote:You might want to look at this small tutorial, it is old but still usable: HowToCreateWebApplicationWithoutAnIDE



Hello,

I have tried to follow this tutorial and now i am trying to run the startup.bat for Tomcat

However, when I do this command line returns:

The JRE_HOME environment variable is not defined correctly
This environment variable is needed to run this program

These are my environment variables:

JAVA_HOME - C:\Program Files\Java\jdk1.8.0_191;
JRE_HOME - C:\Program Files\Java\jre1.8.0_201;
CATALINA_HOME - C:\Program Files\Java\apache-tomcat-9.0.14
CLASSPATH - C:\Program Files\Java\jdk1.8.0_191;\bin;C:\Program Files\Java\jre1.8.0_201;\bin;C:\Program Files\Java\apache-tomcat-9.0.14\lib

These are the results i get when i type in

echo %JAVA_HOME%
echo %JRE_HOME%
echo %CATALINA_HOME%
echo %CLASSPATH%

How can i resolve this issue? Also what is CATALINA_HOME? I've just added it and the JRE_HOME variable as per other suggestions to resolve this issue but still no luck.


 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3241
283
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does java -version in a cmd window give you as a result?

You don't need JRE_HOME.

CATALINA_HOME is tomcat's home directory.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3241
283
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, the CLASSPATH is wrong.

Update your PATH from a cmd window like this:
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frits Walraven wrote:what does java -version in a cmd window give you as a result?

You don't need JRE_HOME.

CATALINA_HOME is tomcat's home directory.





Hello,

Please see my version below.


java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)


There is an entry in program files (x86), tried both for JAVA_HOME and still doesn't seem to work.
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frits Walraven wrote:ok, the CLASSPATH is wrong.

Update your PATH from a cmd window like this:



Sorry you say CLASSPATH is wrong s do i want to change that or PATH?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3241
283
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the CLASSPATH, you do need to update the PATH variable.
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK well i ran that command and it didn't change my path, i'm assuming you mean system variables path - when i checked it wasn't updated and there is several things for that variable - see here

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;%SYSTEMROOT%\System32\OpenSSH\;
 
Nicholas Evans
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nicholas Evans wrote:OK well i ran that command and it didn't change my path, i'm assuming you mean system variables path - when i checked it wasn't updated and there is several things for that variable - see here

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;%SYSTEMROOT%\System32\OpenSSH\;



Also i have now added that path to path system variables but still having same issue.
 
Carey Brown
Saloon Keeper
Posts: 5456
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using 'set' in a command window only sets that variable for the life of the command window. Close the window and open up a new one and it's gone.

You have to go to Control Panel > System > Environment Variables to change it permanently.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3241
283
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

  • open a cmd window from the tomcat directory (the dir where startup.bat is)
  • set PATH=%PATH%;%JAVA_HOME%\bin;%CATALINA_HOME%\bin
  • run startup.bat

  •  
    Carey Brown
    Saloon Keeper
    Posts: 5456
    55
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This probably won't work because Oracle has inserted itself at the beginning of PATH and you're adding JAVA_HOME after that.
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok so i went to this location - C:\Program Files\Java\apache-tomcat-9.0.14\bin

    typed cmd in the address bar which open cmd in that location

    typed set PATH=%PATH%;%JAVA_HOME%\bin;%CATALINA_HOME%\bin

    all it does is return a new line

    then I type startup and same error

    The JRE_HOME environment variable is not defined correctly
    This environment variable is needed to run this program

    Really don't know what to do
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok so,

    I downloaded new version of Java and removed JRE_HOME

    Updated JAVA_HOME and (personal) PATH variable to include the new version of java.

    so now these are my variables:

    PATH - PATH: .;C:\Program Files\Java\apache-tomcat-9.0.14\bin;C:\Program Files\Java\jdk-11.0.2\bin
    JAVA_HOME - C:\Program Files\Java\jdk-11.0.2
    CATALINA_HOME - C:\Program Files\Java\apache-tomcat-9.0.14

    Now startup works -

    I have to run cmd as an administrator.

    Thanks for your help, I'm assuming it was the version of Java I had previously was too old for the version of tomcat?
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:You might want to look at this small tutorial, it is old but still usable: HowToCreateWebApplicationWithoutAnIDE



    Whilst that tutorial was good, it still did not solve my issue with opening my url which is pointing to MyServletA.java

    Do you know how I can resolve this?

    When i click the link in 'this is an amazing servlet' it does not open the java text in the message variable, it just says can't reach this page, yet when I type in MyServlet.java it will display all of the java code
     
    Paul Clapham
    Sheriff
    Posts: 24074
    54
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The code you posted suggests that MyServletA isn't in a named package. You can't use servlets in the default package, so perhaps you need to fix that.
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Paul Clapham wrote:The code you posted suggests that MyServletA isn't in a named package. You can't use servlets in the default package, so perhaps you need to fix that.



    I put it in the same package as the example about tomcat, followed the instructions that i thought were relevant in there. So the code is now:




    compiled it from the src directory

    javac nl\hello\MyServletA.java –d ..\classes

    I adjusted the webconfig file slightly and also created a .war file.

    Changed my test.html to point to the new location of MyServletA.java

    ran startup.bat

    Still same issue.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please post your html file, your web.xml file, and the contents of your war file.
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:Please post your html file, your web.xml file, and the contents of your war file.



    This is my web.xml



    This is my test html

    <HTML>
    <BODY>    
    <a href="file:///C:/Users//Documents/Project/HelloWorld/WEB-INF/src/nl/hello/MyServletA">This is an amazing servlet.</a>  
    </BODY>
    </HTML>

    I removed the user account


    How do I post the contents of the .war file as if I was to open it in notepad ++ it's not really readable.

     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry didn't seem to paste the html correctly

    <a href="file:///C:/Users//Documents/Project/HelloWorld/WEB-INF/src/nl/hello/MyServletA">
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    How do I post the contents of the .war file  


    Sorry, I meant the structure of your war file (like in the small tutorial)

    What is the exact URL you are using again?
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All i am trying to do is open up the test.html which only contains this code plus a " <a href="file:///C:/Users/REMOVED/Documents/Project/HelloWorld/WEB-INF/src/nl/hello/MyServletA">This is an amazing servlet.</a>  " so in my mind when i click on that link it should just return message which contains 'If you're reading this, it worked!" as per below code in MyServletA.java




    when i click on the link it says

    ms-appx-web://microsoft.microsoftedge/assets/errorpages/dnserror.html?ErrorStatus=0x800C0005#file:///C:/Program%20Files/Java/apache-tomcat-9.0.14/webapps/MyServletA/WEB-INF/src/nl/MyServletA

    if i type in

    file:///C:/Program%20Files/Java/apache-tomcat-9.0.14/webapps/MyServletA/WEB-INF/src/nl/

    in my web browser it takes me to that location where MyServletA.java is.

    I feel like what I'm trying to do is more simple than the example in the guide mentioned above.

    The .war file should be structured the same as the example for HelloWorld, i did this as i thought it as i thought it would be more likely to work.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The link in the HTML file is incorrect, you can't point to a file.

    If you check the tutorial again it should be something like:

    http://localhost:8080/HelloWorld/hello



    if your war file is called HelloWorld.war this URL should work from any browser.
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes so locating to http://localhost:8080/MyServletA/ does work - shows what i expect see the screenshot attached:

    but when i click the link it just takes me to http 404 see attached

    MyServletA.png
    [Thumbnail for MyServletA.png]
    404.png
    [Thumbnail for 404.png]
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you change the link in your HTML file to something simular to the tutorial?


    <form method="GET" action="/HelloWorld/hello">
    type some text: <input type="text" name="any" />


    <input type="submit">
    </form>

     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:Did you change the link in your HTML file to something simular to the tutorial?


    <form method="GET" action="/HelloWorld/hello">
    type some text: <input type="text" name="any" />


    <input type="submit">
    </form>




    No I am even more confused haha.

    So i decided to put in what you said into the test.html which is the welcome file in my web.xml which is what contains 'this is an amazing servlet'.

    I changed it nothing happened. I then removed the file from the directory - it was in C:\Program Files\Java\apache-tomcat-9.0.14\webapps\MyServletA

    in this directory i have the .html and two folders META-INF / WEB-INF.

    When i remove the file though and click on http://localhost:8080/MyServletA/ it still sees the page How is this possible as i thought it was opening the page from this directory? This is even after restarting browser / startup.bat

    Also in terms of putting something in correctly i tried to put in

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>Hello World</h1>
    <form method="GET" action="/MyServlet/hello">type some text: <input <br /> type="text" name="any" />



    <input type="SUBMIT"></form>
    </body>
    </html>

    I don't really understand the bold, even from the HelloWorld example because the hierarchy isn't that - HelloWorld\WEB-INF\src\nl\hello the same is for MyServletA as i followed the same instructions - so MyServletA\WEB-INF\src\nl\hello

    Thanks and sorry about this I am quite new to servlets so don't really know what i am doing.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    C:\Program Files\Java\apache-tomcat-9.0.14\webapps\MyServletA  


    I thought you had named your war file HelloWorld.war, so it seems it isn't.... You called it MyServletA.war? This is important because it will determine your contex-root (e.g. starting URL of your web-application)

    The URL servlet-mapping in the web.xml will tell the mapping
    points to your MyServletA servlet.

    In other words, if your war file is called MyServletA.war, then you need the following action: (notice the capital A, I didn't see that in your code)

    <form method="GET" action="/MyServletA/hello">type some text: <input <br /> type="text" name="any" />


     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello, sorry for slow reply I have been busy at work


    This is the web xml so the url pattern is /hello




    My structure for all of my files i think is in the correct place.

    I have my web.xml in \apache-tomcat-9.0.14\webapps\MyServletA\WEB-INF
    I have my MyServletA.war in \apache-tomcat-9.0.14\webapps
    I have my test.html in \apache-tomcat-9.0.14\webapps\MyServletA
    I have my MyServletA.java in \apache-tomcat-9.0.14\webapps\MyServletA\WEB-INF\src\nl\hello
    I have my MyServletA.class in \apache-tomcat-9.0.14\webapps\MyServletA\WEB-INF\classes\nl\hello

    In my book it says to just put the code to point to the java file via an href link which is why i was using the link to the file. Is there absolutely no way of doing that?

    I also don't get why it explains to do it this way in the book if it's not easy to replicate.

    This is in Head first java btw.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So, what happens if you go to the following URL in a browser when Tomcat is running?

    http://localhost:8080/MyServletA/hello
     
    Nicholas Evans
    Greenhorn
    Posts: 26
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Frits Walraven wrote:So, what happens if you go to the following URL in a browser when Tomcat is running?

    http://localhost:8080/MyServletA/hello



    It takes me to what i expect which is

    If you're reading this, it worked! - which is in the java.

    Thanks so much for your help, I've changed to .html file and now it is working correctly.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3241
    283
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Congratulations  

    You didn't give up and that deserves a Cow!
     
    How do they get the deer to cross at the signs? Or to read this tiny ad?
    java tutor first lesson for free
    https://coderanch.com/t/702613/java-tutor-lesson-free
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!