java - Why the servlet doesn't work? -
all, newbie java web development, trying implement servlet in test. found servlet created doesn't work . didn't know whether had missed anything. please me review .thanks.
what had done far is:
- created
dynamic web projectnamedsecondweboptiongenerate web.xml dd. - added
servletnamedhelloservletunder packagecom.example.servlets. configurl mappingvalues/helloservlet,/*. hope work url pattern under root.
here code .
package com.example.servlets; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class servlet: helloservlet * */ public class helloservlet extends javax.servlet.http.httpservlet implements javax.servlet.servlet { /* (non-java-doc) * @see javax.servlet.http.httpservlet#httpservlet() */ public helloservlet() { super(); } /* (non-java-doc) * @see javax.servlet.http.httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.getwriter().write("hello, world!"); } /* (non-java-doc) * @see javax.servlet.http.httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub } } and added index.jsp joined test.
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!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>my title</title> </head> <body> <% java.util.date d = new java.util.date(); %> <h1> today's date <%= d.tostring() %> , jsp page worked! </h1> </body> </html> what expected hope hello world string can added index.jsp response html when access url http://localhost:8080/secondweb or http://localhost:8080/secondweb/index.jsp. seems helloservlet doesn't work. why? thanks.
i found issue, have add servlet configuration in web.xml. below under root element:
<servlet> <servlet-name>helloservlet</servlet-name> <servlet-class>com.example.servlets.helloservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Comments
Post a Comment