gudii9
asked on
servlet example
Hi,
i created Sample servlet at
C:\gpSoftwares\apache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\gpP rojects\WE B-INF\clas ses
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
i went to coomand prompt and tried to compile
i got below 7 errors
C:\gpSoftwares\apache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\gpP rojects\WE B-INF\clas ses>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-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\gpP rojects\WE B-INF\clas ses>set classpath=C:\gpSoftwares\a pache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\l ib\servlet -api;.;
still getting same errors
C:\gpSoftwares\apache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\gpP rojects\WE B-INF\clas ses>set classpath=C:\gpSoftwares\a pache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\l ib\servlet -api;.;
C:\gpSoftwares\apache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\gpP rojects\WE B-INF\clas ses>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
i created Sample servlet at
C:\gpSoftwares\apache-tomc
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>");
}
}
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>
i went to coomand prompt and tried to compile
i got below 7 errors
C:\gpSoftwares\apache-tomc
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-tomc
still getting same errors
C:\gpSoftwares\apache-tomc
C:\gpSoftwares\apache-tomc
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
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.");
}
}
if i change it worked as attached
classs.png
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
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
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
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
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.
I can't tell what the question is. Seems like your rambling on with yourself.
gudii9 - please post actual questions - not a running commentary on your work. Otherwise nothing is likely to get answered
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
http://localhost:50000/gpProjects/sample
above worked fine
Since loading, this servlet has been accessed 1 times.
worked.png
above worked fine
Since loading, this servlet has been accessed 1 times.
worked.png
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
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-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17\w ebapps\exa mples\WEB- INF\web.xm l
?
C:\gpSoftwares\apache-tomc
?
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">
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">
ASKER
you mean my web.xml should be as below?
what is importance of root tag?
can i refer same tomcat instance from eclipse as well running at below url?
C:\gpSoftwares\apache-tomc at-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17
<?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>
what is importance of root tag?
can i refer same tomcat instance from eclipse as well running at below url?
C:\gpSoftwares\apache-tomc
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-tomcThat 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.batat-9.0.0.M 17-windows -x64\apach e-tomcat-9 .0.0.M17
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
ASKER
corrected above as below
by ading ,jar
also class changed to Clas
now i see 12 errors
C:\gpSoftwares\apache-tomc
C:\gpSoftwares\apache-tomc
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-tomc
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-tomc