日期:2014-05-16  浏览次数:20392 次

JSP学习 Hello world问题解决

Servlet在浏览器中的输出

第一次:guest/test目录不存在

原因是web.xml没有被正确解析,修改之

第二次:HTTP method get is not supperted by this URL

(继承自httpservlet的servlet没有重写对于请求和相应的处理方法,doget或dopost登方法,默认调用父类的doget和dopost方法)

?

解决办法:

1:子类重写doget和dopost方法

2:在扩展的servlet中重写doget或dopost方法或在处理时不调用父类的doget和dopost方法

?

package station.guest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Users extends HttpServlet {
 
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doGet(req, resp);
		PrintWriter out = resp.getWriter();
		out.print("hello money");
	}

}

?

?

配置文件

?<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
	<servlet>
		<servlet-name>user</servlet-name>
		<display-name>users </display-name>
		<servlet-class>station.guest.Users</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>user</servlet-name>
		<url-pattern>/test/* </url-pattern>
	</servlet-mapping>
</web-app>
?