若能解决肯定结贴 servlet 留言板 The requested resource is not available

fire_fly0 2015-08-12 04:38:14
各位大神帮帮忙,我是要做一个留言板用JSP+servlet,可是老师出现这个问题
type Status report

message /CH009/AddMessageServlet

description The requested resource is not available.

Apache Tomcat/7.0.57

以下是我的JSP和servlet代码

<%@ page language="java" import="java.util.*" contentType="text/html" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>留言板面</title>



</head>

<body background="pic/background.jpg">
<form action="AddMessageServlet" method="post">
留言者:<input type="text" name="author" size="30"/><br>
留言标题:<input type="text" name="title" size="30"/><br>
留言内容:<textarea name="content" rows="8" cols="30"/></textarea>
<p>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
<a href="showMessage.jsp">查看留言</a>
</p>
</form>

</body>
</html>


package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

public class AddMessageServlet extends HttpServlet {

public AddMessageServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

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

doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String author=new
String(request.getParameter("author").getBytes("ISO-8859-1"),"UTF-8");
String title=new
String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8");
String content=new
String(request.getParameter("content").getBytes("ISO-8859-1"),"UTF-8");
SimpleDateFormat format=new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String today=format.format(new Date());
MessageBean mm=new MessageBean();
mm.setAuthor(author);
mm.setTitle(title);
mm.setTime(today);
mm.setContent(content);
HttpSession session=request.getSession();
ServletContext scx=session.getServletContext();
ArrayList wordlist=(ArrayList)scx.getAttribute("wordlist");
if(wordlist==null)
wordlist=new ArrayList();
wordlist.add(mm);
scx.setAttribute("wordlist", wordlist);
response.sendRedirect("showMessage.jsp");
}
public void init() throws ServletException {
// Put your code here
}

}
*********************************************************************************************

<%@ page import="servlet.MessageBean" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
<head>
<base href="<%=basePath%>">

<title>显示留言内容</title>
</head>

<body background="pic/background.jpg">
<%
ArrayList wordlist=(ArrayList)application.getAttribute("wordlist");
if(wordlist==null||wordlist.size()==0)
out.print("没有留言可以显示!");
else{
for(int i=wordlist.size()-1;i>=0;i--){
MessageBean mm=(MessageBean)wordlist.get(i);
%>
留言者:<%=mm.getAuthor() %>
<p>留言时间:<%=mm.getTime() %></p>
<p>留言标题:<%=mm.getTitle() %></p>
<p>留言内容:
<textarea rows="8" cols="30" readonly>
<%=mm.getContent() %>
</textarea>
</p>
<a href="messageBoard.jsp">我要留言</a>
<hr width="90%">
<%
}
}
%>
</body>
</html>

**************************************************************************************************
package servlet;

public class MessageBean {
private String author;
private String title;
private String content;
private String time;
public MessageBean() {
super();
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}


}
...全文
135 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ITjavaman 2015-08-14
表单提交的路径错了,你XML文件映射的地址是<url-pattern>/servlet/AddMessageServlet</url-pattern> 所以表单的action应该是servlet/AddMessageServlet
回复
fire_fly0 2015-08-14
谢谢大家可算解决了
回复
Java碎碎念 2015-08-14
刚本地测了下,jsp中form的action写错了,改成如下即可:
<form action="<%=request.getContextPath() %>/servlet/AddMessageServlet" method="post">
		留言者:<input type="text" name="author" size="30" /><br> 留言标题:<input
			type="text" name="title" size="30" /><br> 留言内容:
		<textarea name="content" rows="8" cols="30" /></textarea>
		<p>
			<input type="submit" value="提交" /> <input type="reset" value="重置" />
			<a href="showMessage.jsp">查看留言</a>
		</p>
	</form>
回复
fire_fly0 2015-08-13
配置也看不出来哪里错啊
回复
fire_fly0 2015-08-13
这是我的web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <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>Verify</servlet-name> <servlet-class>servlet.Verify</servlet-class> </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>ShowMessage</servlet-name> <servlet-class>servlet.ShowMessage</servlet-class> </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>AddMessageServlet</servlet-name> <servlet-class>servlet.AddMessageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Verify</servlet-name> <url-pattern>/servlet/Verify</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowMessage</servlet-name> <url-pattern>/servlet/ShowMessage</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AddMessageServlet</servlet-name> <url-pattern>/servlet/AddMessageServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
回复
兔子托尼啊 2015-08-12
Destiny_ning 2015-08-12
web.xml贴出来看看
回复
KBoxJinJin 2015-08-12
你用提交表单做吧 这样子 直接点连接没有传参数呀。
回复
你的AddMessageServlet配置在哪
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-12 04:38
社区公告
暂无公告