Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

servlet example

Hi,

i created Sample servlet at

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!!!!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

Open in new window


i created basically
WEB-INF under gpProjects

under WEB_INF i created classes under which i created above Sample.java servlet
under WEB_INF i wrote web,xml as
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">
  <servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>Sample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/sample</url-pattern>
  </servlet-mapping>  
</web-app>

Open in new window


i went to coomand prompt and tried to compile
i got below 7 errors

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>javac Sample.java
Sample.java:5: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld extends HttpServlet {
       ^
Sample.java:2: error: package javax.servlet does not exist
import javax.servlet.*;
^
Sample.java:3: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Sample.java:5: error: cannot find symbol
public class HelloWorld extends HttpServlet {
                                ^
  symbol: class HttpServlet
Sample.java:7: error: cannot find symbol
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                      ^
  symbol:   class HttpServletRequest
  location: class HelloWorld
Sample.java:7: error: cannot find symbol
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                  ^
  symbol:   class HttpServletResponse
  location: class HelloWorld
Sample.java:8: error: cannot find symbol
    throws IOException, ServletException
                        ^
  symbol:   class ServletException
  location: class HelloWorld
7 errors


i set classpath to servlet-api.jar
C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>set classpath=C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\lib\servlet-api;.;
still getting same errors
C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>set classpath=C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\lib\servlet-api;.;

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>javac Sample.java
Sample.java:5: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld extends HttpServlet {
       ^
Sample.java:2: error: package javax.servlet does not exist
import javax.servlet.*;
^
Sample.java:3: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Sample.java:5: error: cannot find symbol
public class HelloWorld extends HttpServlet {
                                ^
  symbol: class HttpServlet
Sample.java:7: error: cannot find symbol
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                      ^
  symbol:   class HttpServletRequest
  location: class HelloWorld
Sample.java:7: error: cannot find symbol
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                  ^
  symbol:   class HttpServletResponse
  location: class HelloWorld
Sample.java:8: error: cannot find symbol
    throws IOException, ServletException
                        ^
  symbol:   class ServletException
  location: class HelloWorld
7 errors

how to resolve this
str.png
coming2.png
coming.png
port.png
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>set classpath=C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\lib\servlet-api;.;

corrected above as below

by ading ,jar

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>set classpath=C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\lib\servlet-api.jar;.;

also class changed to Clas
now i see 12 errors


C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>set classpath=c:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\lib\servlet-api.jar;.;

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>javac Sample.java
Sample.java:5: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld extends HttpServlet {
       ^
1 error

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>javac Sample.java
Sample.java:5: error: class, interface, or enum expected
public Class HelloWorld extends HttpServlet {
       ^
Sample.java:7: error: class, interface, or enum expected
    public void doGet(HttpServletRequest request, HttpServletResponse response)
           ^
Sample.java:11: error: class, interface, or enum expected
        PrintWriter out = response.getWriter();
        ^
Sample.java:12: error: class, interface, or enum expected
        out.println("<html>");
        ^
Sample.java:13: error: class, interface, or enum expected
        out.println("<head>");
        ^
Sample.java:14: error: class, interface, or enum expected
        out.println("<title>Hello World!!!!</title>");
        ^
Sample.java:15: error: class, interface, or enum expected
        out.println("</head>");
        ^
Sample.java:16: error: class, interface, or enum expected
        out.println("<body>");
        ^
Sample.java:17: error: class, interface, or enum expected
        out.println("<h1>Hello World!</h1>");
        ^
Sample.java:18: error: class, interface, or enum expected
        out.println("</body>");
        ^
Sample.java:19: error: class, interface, or enum expected
        out.println("</html>");
        ^
Sample.java:20: error: class, interface, or enum expected
    }
    ^
12 errors

C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\gpProjects\WEB-INF\classes>
Avatar of gudii9

ASKER

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Sample extends HttpServlet {

  int count = 0;

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;
    out.println("Since loading, this servlet has been accessed " +
                count + " times.");
  }
}

Open in new window


if i change it worked as attached
classs.png
Avatar of gudii9

ASKER

when i hit below

http://localhost:50000/gpProjects/sample

i get error as attachded
HTTP Status 404 - /gpProjects/sample

type Status report

message /gpProjects/sample

description The requested resource is not available.

Apache Tomcat/9.0.0.M17
classserr.png
Avatar of gudii9

ASKER

when i go here to
http://localhost:50000/manager/html

and click on gpProjects reload button



gpProjects      None specified             true      0       Start   Stop  Reload  Undeploy
 Expire sessions with idle ≥
30
 minutes


i get error as




403 Access Denied

You are not authorized to view this page.

By default the Manager is only accessible from a browser running on the same machine as Tomcat. If you wish to modify this restriction, you'll need to edit the Manager's context.xml file.

If you have already configured the Manager application to allow access and you have used your browsers back button, used a saved book-mark or similar then you may have triggered the cross-site request forgery (CSRF) protection that has been enabled for the HTML interface of the Manager application. You will need to reset this protection by returning to the main Manager page. Once you return to this page, you will be able to continue using the Manager application's HTML interface normally. If you continue to see this access denied message, check that you have the necessary permissions to access this application.

If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.

For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.

<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.

manager-gui - allows access to the HTML GUI and the status pages
manager-script - allows access to the text interface and the status pages
manager-jmx - allows access to the JMX proxy and the status pages
manager-status - allows access to the status pages only
The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:

Users with the manager-gui role should not be granted either the manager-script or manager-jmx roles.
If the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session.
For more information - please see the Manager App HOW-TO.
err2.png
Avatar of gudii9

ASKER

not able to integrate eclipise neon with tomcat 9 as well nt sure options disabled under server view?
Oh my god!
I can't tell what the question is. Seems like your rambling on with yourself.
Avatar of CEHJ
gudii9 - please post actual questions - not a running commentary on your work. Otherwise nothing is likely to get answered
Avatar of gudii9

ASKER

my questions is why my Sample servlet is not showing
when i go to below url i see
http://localhost:50000/gpProjects/Sample

i see below error
HTTP Status 404 - /gpProjects/Sample

type Status report

message /gpProjects/Sample

description The requested resource is not available.

Apache Tomcat/9.0.0.M17

my project structure is as atached under
webapps i have my gpProjects
under that
WEB-INF under that i have classes and lib directory


please advise
gpProjects.png
gpProjects2.png
gpProjects3.png
gpProjects4.png
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

http://localhost:50000/gpProjects/sample
above worked fine

Since loading, this servlet has been accessed 1 times.
worked.png
Avatar of gudii9

ASKER

i checked
here

http://localhost:50000/examples/servlets/

no web.xml and root tag

i only see src  of servle but not web.xml
webxml.png
worked.png
Are you looking for a web.xml file? Isn't there one at
C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\examples\WEB-INF\web.xml    
?
Avatar of gudii9

ASKER

i see there

is below is root tag?

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">
Avatar of gudii9

ASKER

you mean my web.xml should be as below?

<?xml version="1.0" encoding="UTF-8"?>
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">
  <servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>Sample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/sample</url-pattern>
  </servlet-mapping>  
</web-app>

Open in new window


what is importance of root tag?

can i refer same tomcat instance from eclipse as well running at below url?
C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17
is below is root tag?
Yes, that is good. In the future, sometime you will change metadata-complete attribute to metadata-complete="false"  but leave it as is for now.
you mean my web.xml should be as below?
Yes.
what is importance of root tag?
Tomcat looks at it to decide on how to treat your web app.  
can i refer same tomcat instance from eclipse as well running at below url?
I think so. But, let Eclipse start and stop it.
C:\gpSoftwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17
That path is kind of difficult to use all the time. I suggest that you move the "binary" distribution of Tomcat into a different folder.  You could just use something like C:\Tomcat. That way your paths are easier to type. For example, C:\Tomcat\bin\startup.bat
Avatar of gudii9

ASKER

can i refer same tomcat instance from eclipse as well running at below url?
I think so. But, let Eclipse start and stop it.

probably i will download and put different tomcat for that