关于Servlet的优化封装

发布于 2021-11-10 22:50:21 字数 111 浏览 922 评论 3

java的web项目刚才入门...

在项目开发开发中..发现新建一个Servlet类只是针对一个Action来做相应处理..

感觉烦琐...

求一封装思路...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2021-11-13 18:24:54

我框架中的控制中心

/*
 * Copyright @ 2006-2010 by The Jxva Framework Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jxva.mvc;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

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

import com.jxva.entity.Encoding;
import com.jxva.mvc.entity.BaseServlet;
import com.jxva.mvc.entity.View;
import com.jxva.mvc.util.ControllerUtil;
import com.jxva.mvc.util.TemplateUtil;
import com.jxva.plugin.MvcPlugin;
import com.jxva.plugin.SysPlugin;
import com.jxva.util.FileUtil;

/**
 * framework core controller<br>
 * 根据用户请求调用相应的业务处理类,并根据执行结果或注解值导航<br>
 * 需要在web.xml中配置
 * <pre>
 * <servlet>
 *   <servlet-name>Controller</servlet-name>
 *     <servlet-class>com.jxva.mvc.Controller</servlet-class>
 *   </servlet>
 *   <servlet-mapping>
 *     <servlet-name>Controller</servlet-name>
 *     <url-pattern>*.jv</url-pattern>
 *  </servlet-mapping>
 * </pre>
 * @author  The Jxva Framework
 * @since   1.0
 * @version 2008-11-27 10:00:53 by Jxva
 */
public class Controller extends BaseServlet{
	
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
//		response.setHeader("Pragma", "No-cache");
//		response.setHeader("Cache-Control", "no-cache");
//		response.setDateHeader("Expires", 0);
		try{
			final String[] actionMethod=ControllerUtil.getActionAndMethod(request.getServletPath());
			final Class<?> action=MvcPlugin.getAction(actionMethod[0]);
			if(action==null){
				int lastPos=actionMethod[0].lastIndexOf('/')+1;
				View.printInfoWithHeaderAndFooter(response.getWriter(),"There is no Action mapped for namespace "+actionMethod[0].substring(0,lastPos)+" and action name '"+actionMethod[0].substring(lastPos)+"'.");
				return;
			}
			final Method method=action.getMethod(actionMethod[1]);
			if(response.isCommitted()){
				return;
			}else{
				if(method.getAnnotations().length==0){
					final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
					if(result==null){
						View.printInfoWithHeaderAndFooter(response.getWriter(),action.getName()+'.'+actionMethod[1]+"() can't return null.");
					}else{
						View.printInfo(response.getWriter(),result);
					}
				}else{
					if(method.isAnnotationPresent(Forward.class)){
						Forward forward=method.getAnnotation(Forward.class);
						if(forward.cache()==0||request.getParameter(ControllerUtil.FORWARD_FLAG)!=null){//非缓存
							final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
							forward(request,response,ControllerUtil.getValue(result,forward.value()));
						}else{//采用缓存
							File file=ControllerUtil.getCacheFile(request);
							if(file.exists()){
								if(forward.cache()>0){
									if(System.currentTimeMillis()-file.lastModified()>forward.cache()*1000){
										ControllerUtil.createForwardCache(request,file);
									}
								}
							}else{
								ControllerUtil.createForwardCache(request,file);
							}
							forward(request,response,SysPlugin.CACHE_PATH+file.getName());
						}
					}else if(method.isAnnotationPresent(Redirect.class)){
						final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
						redirect(request,response,ControllerUtil.getValue(result,method.getAnnotation(Redirect.class).value()));
					}else if(method.isAnnotationPresent(Template.class)){
						Template template=method.getAnnotation(Template.class);
						if(template.cache()==0){//非缓存
							final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
							View.printInfo(response.getWriter(),TemplateUtil.execute(ControllerUtil.getValue(result,template.value())));
						}else{//采用缓存
							File file=ControllerUtil.getCacheFile(request);
							if(file.exists()){
								if(template.cache()>0){
									if(System.currentTimeMillis()-file.lastModified()>template.cache()*1000){
										final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
										FileUtil.write(file,TemplateUtil.execute(ControllerUtil.getValue(result,template.value())),Encoding.UTF_8);
									}
								}
							}else{
								final String result=ControllerUtil.execute(action,actionMethod[1],request,response);
								FileUtil.write(file,TemplateUtil.execute(ControllerUtil.getValue(result,template.value())),Encoding.UTF_8);
							}
							forward(request,response,SysPlugin.CACHE_PATH+file.getName());
						}
					}else if(method.isAnnotationPresent(Perform.class)){
						ControllerUtil.execute(action,actionMethod[1],request,response);
						return;
					}
				}
			}
		}catch(Exception e){
			try{
				e.printStackTrace();
				if(SysPlugin.DEBUG){
					View.printExceptionWithHeaderAndFooter(response.getWriter(),e);
				}else{
					forward(request,response,SysPlugin.ERROR_PAGE);
				}
			}catch(IOException ex){
				ex.printStackTrace();
			}
		}
	}
}

柳絮泡泡 2021-11-12 09:17:43

可以看看 OSChina 的处理方法:

http://www.oschina.net/bbs/thread/8225

谁的新欢旧爱 2021-11-11 17:17:20

参考struts2的思路自己实现servlet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文