简体   繁体   English

如何使JSP页面扩展抽象HttpServlet

[英]How to make JSP page extend abstract HttpServlet

I'm working on a Web application that works with multiple tables in a database. 我正在开发一个可以处理数据库中多个表的Web应用程序。 The design requirements specify that each table have its own page to work with. 设计要求指定每个表都有自己的页面可供使用。 For the most part, a good chunk of the server-side code is the same across all the pages, with the page specific stuff mostly dealing with the sql for the specific tables (and they all have different structures, BTW). 在大多数情况下,服务器端代码的很大一部分在所有页面中是相同的,页面特定的东西主要处理特定表的sql(并且它们都具有不同的结构,BTW)。

So I thought it would be prudent to organize the code so that all the common methods were all in one shared place and leaving only the unique implementations on the JSP pages. 所以我认为组织代码是明智的,这样所有常用方法都在一个共享位置,只留下JSP页面上的唯一实现。

The structure I have attempted to create is to have an abstract superclass, which we will call MyServlet , that extends HttpServlet . 我试图创建的结构是有一个抽象的超类,我们称之为MyServlet ,它扩展了HttpServlet Each of my JSP pages extend this superclass. 我的每个JSP页面都扩展了这个超类。

However when I went to test this out my pages now fail to load with HTTP 405 Method not allowed error. 但是,当我去测试时,我的页面现在无法加载HTTP 405 Method not allowed错误。

For security/business reasons I cannot simply copy and paste my code here, but I can post a general structure. 出于安全/商业原因,我不能简单地复制和粘贴我的代码,但我可以发布一般结构。 My Servlet class looks something like this: 我的Servlet类看起来像这样:

public abstract class MyServlet extends HttpServlet
{
     public void someMethod()
     {
          abstractMethod();
     }

     protected abstract void abstractMethod();
}

And my jsp like this: 我的jsp是这样的:

<%@ extends="package.MyServlet" %>
<%! 
     public void abstractMethod()
     {
          someCode;
     }
%>
<% someMethod() %>

I should point out that MyServlet does not implement doGet() or doPost() . 我应该指出MyServlet没有实现doGet()doPost() I was under the assumption that with JSP pages themselves being extensions of HttpServlet that they provided implementations for those methods, though I am also aware that error 405 happens because at least doGet() is not there. 我假设JSP页面本身是HttpServlet扩展,他们为这些方法提供了实现,但我也知道错误405的发生是因为至少doGet()不存在。

Is the error happening because I have not implemented doGet() , and if so how do I implement it such that it works with my JSP pages? 是否发生了错误,因为我没有实现doGet() ,如果是这样,我如何实现它以使它适用于我的JSP页面? Or is there some other reason why I am getting this error? 或者是否有其他原因导致我收到此错误?

can you just create your method in some utility class and then include it, instead of extending it? 你能在一些实用程序类中创建你的方法然后包含它,而不是扩展它吗?

<%@ page import="org.foo.HelperClass, org.foo.StaticHelper" %>
 <%! 
 HelperClass helper = new HelperClass();
 %>
 Helper Stuff: <%= helper.someMethod() %><br/>
 Static Stuff: <%= StaticHelper.someStaticMethod() %>

would this accomplish your goal? 这会实现你的目标吗?

I like this basic orgainzation of a servlet and JSP web application 我喜欢servlet和JSP Web应用程序的基本组合

  1. Business code (including database access) is in the servlet (or a business layer that is called by the servlet). 业务代码(包括数据库访问)位于servlet(或servlet调用的业务层)中。
  2. JSP is exclusively for presentation of data and retrieval of user input. JSP专门用于呈现数据和检索用户输入。

If you have a resultSet and want to present it, load the information that must be presented into a Collection (I like List) and set it in an appropriate scope (session, page, request, most likely not application). 如果您有一个resultSet并想要呈现它,请将必须呈现的信息加载到Collection(我喜欢List)并将其设置在适当的范围内(会话,页面,请求,很可能不是应用程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM