I can not seem to get the following basic java servlet to run, this was given as an example.Can someone please tell me if I am missing something, when I run the basic tomcat index.jsp file using: http://localhost:8080/index.jsp, then it works perfectly,so I think Ive set tomcat7 up correctly. When I type in the following url: http://localhost:8080/HelloWorld.java then the browser asks if I want to download the file and if I choose to open the file with a browser then it just shows me the source code.
All my created .jsp files run but servlets are all giving me this behavior, I have reinstalled several times, I think this is probably due to my ignorance in some department, can anyone please give me advice?
This servlet(HelloWorld.java) and its compiled class(HelloWorld.class) are located within the default ROOT folder within tomcat:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\
The web.xml located below file is located within the following older:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\
My HelloWorld.java servlet:
package org.kodejava.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public HelloWorld() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head><title>Hello World Servlet</title></head>");
writer.println("<body>Hello World! How are you doing?</body>");
writer.println("</html>");
writer.close();
}
}
The web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>kodejava-example</display-name>
<servlet>
<description></description>
<display-name>HelloWorld</display-name>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>
org.kodejava.example.servlet.HelloWorld
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>

New Topic/Question
Reply



MultiQuote






|