Servlet如何将控制权转交给另一个页面

dingding_hi 2009-12-15 03:53:48
package com.zxm.web;

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

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

public class CompIntrDeal extends HttpServlet {

public CompIntrDeal() {
super();
}


public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
rd.forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

public void init() throws ServletException {
}

}


本来预期结果是跳转到header.html页面。
但浏览器中却显示了下面的一条消息,实际上转到的并不是head.html页面:
引用
This is class com.zxm.web.CompIntrDeal, using the POST method

...全文
183 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dragon_java_li 2009-12-21
  • 打赏
  • 举报
回复
把action值改成post
SambaGao 2009-12-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wwwtyb 的回复:]
如果Servlet配置没有问题的话,
可能是
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
这个地方跳转页面路径的问题
改成:RequestDispatcher rd = request.getRequestDispatcher("header.html");
试试看....
[/Quote]

试试
aloyise 2009-12-20
  • 打赏
  • 举报
回复
估计是 那个servlet的mapping没写好
hwzjava 2009-12-19
  • 打赏
  • 举报
回复
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
rd.forward(request, response);
crazylaa 2009-12-19
  • 打赏
  • 举报
回复
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
rd.forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

换一下试试看:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
rd.forward(request, response);

}


funlinihao 2009-12-19
  • 打赏
  • 举报
回复
考虑./admin/CompIntrDeal
dingding_hi 2009-12-15
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<context-param>
<param-name>host</param-name>
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>database</param-name>
<param-value>website</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123</param-value>
</context-param>
<context-param>
<param-name>JDBCDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>dbPort</param-name>
<param-value>3306</param-value>
</context-param>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CompIntrDeal</servlet-name>
<servlet-class>com.zxm.web.CompIntrDeal</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CompIntrDeal</servlet-name>
<url-pattern>/admin/CompIntrDeal</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
fdimaof 2009-12-15
  • 打赏
  • 举报
回复
试试看了
wwwtyb 2009-12-15
  • 打赏
  • 举报
回复
如果Servlet配置没有问题的话,
可能是
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
这个地方跳转页面路径的问题
改成:RequestDispatcher rd = request.getRequestDispatcher("header.html");
试试看....
believefym 2009-12-15
  • 打赏
  • 举报
回复
把整个web.xml贴出来看看?
dingding_hi 2009-12-15
  • 打赏
  • 举报
回复
/admin/compIntr.jsp中form表单的部分内容:
out.println("<form action=\"CompIntrDeal\" method=\"post\">");

web.xml的servlet映射:

<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CompIntrDeal</servlet-name>
<servlet-class>com.zxm.web.CompIntrDeal</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CompIntrDeal</servlet-name>
<url-pattern>/admin/CompIntrDeal</url-pattern>
</servlet-mapping>

映射应该是没有什么问题的,都是通过myeclipse自动产生的
believefym 2009-12-15
  • 打赏
  • 举报
回复
form的action是怎么写的,
web.xml里servlet映射好了吗?
Trinx 2009-12-15
  • 打赏
  • 举报
回复
java技术交流群100756746,希望大家加入,我们共同讨论,共同进步!
dingding_hi 2009-12-15
  • 打赏
  • 举报
回复
我的servlet源代码是这样的:

package com.zxm.web;

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

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

public class CompIntrDeal extends HttpServlet {

public CompIntrDeal() {
super();
}


public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/admin/header.html");
rd.forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

public void init() throws ServletException {
}

}


81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧