拜托高手来看看,第一次写JSP+Servlet的程序,也不知道哪里出错的,代码都贴出来。
**************************下面是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"> <filter>
<filter-name>EncodeFilter</filter-name>
<filter-class>org.cumt.Student.Filter.EncodeFilter</filter-class>
<init-param>
<param-name>Encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>NextServlet</servlet-name>
<servlet-class>org.cumt.Student.Control.NextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NextServlet</servlet-name>
<url-pattern>/next</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NextServlet</servlet-name>
<url-pattern>/show.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>show.jsp</welcome-file>
</welcome-file-list>
</web-app>
*************************下面是NextServlet的代码**************************
package org.cumt.Student.Control;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.cumt.Student.Model.Action.AddAction;
import org.cumt.Student.Model.Action.EditAction;
import org.cumt.Student.Model.Action.IAction;
import org.cumt.Student.Model.Action.SaveAction;
import org.cumt.Student.Model.Action.ShowAction;@SuppressWarnings("serial")
public class NextServlet extends HttpServlet {
public void init() throws ServletException {
this.getServletContext().setAttribute("show", new ShowAction("/show.jsp"));
this.getServletContext().setAttribute("insert", new AddAction("/InsertData.htm"));
this.getServletContext().setAttribute("add", new AddAction("/next?cmd=show"));
this.getServletContext().setAttribute("edit",new EditAction("/EditData.jsp"));
this.getServletContext().setAttribute("save", new SaveAction("/next?cmd=show"));
} public void destroy() {
this.getServletContext().removeAttribute("show");
this.getServletContext().removeAttribute("insert");
this.getServletContext().removeAttribute("add");
this.getServletContext().removeAttribute("edit");
this.getServletContext().removeAttribute("save");
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd=request.getParameter("cmd");
String next=null;
if(cmd!=null){
IAction action=(IAction) this.getServletContext().getAttribute(cmd);
next=action.execute(request);
}else{
next="/error.jsp";
}
this.getServletContext().getRequestDispatcher(next).forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
**************************下面是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"> <filter>
<filter-name>EncodeFilter</filter-name>
<filter-class>org.cumt.Student.Filter.EncodeFilter</filter-class>
<init-param>
<param-name>Encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>NextServlet</servlet-name>
<servlet-class>org.cumt.Student.Control.NextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NextServlet</servlet-name>
<url-pattern>/next</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NextServlet</servlet-name>
<url-pattern>/show.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>show.jsp</welcome-file>
</welcome-file-list>
</web-app>
*************************下面是NextServlet的代码**************************
package org.cumt.Student.Control;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.cumt.Student.Model.Action.AddAction;
import org.cumt.Student.Model.Action.EditAction;
import org.cumt.Student.Model.Action.IAction;
import org.cumt.Student.Model.Action.SaveAction;
import org.cumt.Student.Model.Action.ShowAction;@SuppressWarnings("serial")
public class NextServlet extends HttpServlet {
public void init() throws ServletException {
this.getServletContext().setAttribute("show", new ShowAction("/show.jsp"));
this.getServletContext().setAttribute("insert", new AddAction("/InsertData.htm"));
this.getServletContext().setAttribute("add", new AddAction("/next?cmd=show"));
this.getServletContext().setAttribute("edit",new EditAction("/EditData.jsp"));
this.getServletContext().setAttribute("save", new SaveAction("/next?cmd=show"));
} public void destroy() {
this.getServletContext().removeAttribute("show");
this.getServletContext().removeAttribute("insert");
this.getServletContext().removeAttribute("add");
this.getServletContext().removeAttribute("edit");
this.getServletContext().removeAttribute("save");
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd=request.getParameter("cmd");
String next=null;
if(cmd!=null){
IAction action=(IAction) this.getServletContext().getAttribute(cmd);
next=action.execute(request);
}else{
next="/error.jsp";
}
this.getServletContext().getRequestDispatcher(next).forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
解决方案 »
- java内存泄漏疑问
- AIX 系统下JDK1.4环境 运行JAVA进程,log4j-1.2.8 版本出错,求解决
- JAVA可以处理GIF动画吗
- super.clone()
- 请教一个Applet运行结果的问题?
- 求一段数据库结果集保存到ArrayList的写法?在线等~帮顶有分
- non-static method doSaveDataImexport() cannot be referenced from a static context 请问是什么意思?我该怎样解决?代码见内,谢谢
- 认证模拟。
- 请问struts中FormBean和ActionForm有什么区别?谢谢!!!!
- jbuilder4.0卸不了
- 想从request域中提取集合list的信息,但是出错的。
- 关于泛型的一个问题
<servlet>
<servlet-name>NextServlet </servlet-name>
<servlet-class>org.cumt.Student.Control.NextServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NextServlet </servlet-name>
<url-pattern>/next </url-pattern>
</servlet-mapping><!-- <servlet-mapping>
<servlet-name>NextServlet </servlet-name>
<url-pattern>/show.jsp </url-pattern>
</servlet-mapping> 去掉这个-->访问的时候这样:
http://localhost:8080/yourapp/next
**************************下面是Filter的Java代码****************************
package org.cumt.Student.Filter;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class EncodeFilter implements Filter{
protected FilterConfig config=null;
protected String encodeStr;
public void init(FilterConfig filterConfig) throws ServletException {
this.config=filterConfig;
this.encodeStr=config.getInitParameter("Encode");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(encodeStr!=null){
request.setCharacterEncoding(encodeStr);
response.setContentType("text/html; charset="+encodeStr);
} chain.doFilter(request, response);
}
public void destroy() {
this.encodeStr=null;
this.config=null;
}
}
package com.controller;import java.io.IOException;
import javax.servlet.*;public class SetCharacterEncodingFilter implements Filter { protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; // destroy方法
public void destroy() {
this.encoding = null;
this.filterConfig = null; } // 选择设置使用的字符编码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
} // Pass control on to the next filter
chain.doFilter(request, response); } // 将这个filter放置在服务器中
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }
}