下面只说业务逻辑层
首先要有一个类继承自HttpServlet类 并重写里面的方法
package com.zhy.t502.entity; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhy.t502.biz.Newsbiz; import com.zhy.t502.biz.Topicbiz; import com.zhy.t502.biz.impl.Newsbizimpl; import com.zhy.t502.biz.impl.Topicbizimpl; public class Fenye extends HttpServlet { private Newsbiz nb = new Newsbizimpl(); private Topicbiz top = new Topicbizimpl(); /** * Constructor of the object. */ public Fenye() { super(); } /** * Destruction of the servlet. <br> */ 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 { //根据主题查看新闻 // 取集合 // 国际新闻 List gj =nb.findnew(1, 5, 2); // 国内新闻 List gn =nb.findnew(1, 5, 1); // 标题集合 List bt =top.findtop(); // 新闻集合 String pn=request.getParameter("pn"); System.out.println(pn); List xw = nb.findnew(Integer.parseInt(pn), 5); // 存集合 request.setAttribute("xw", xw); request.setAttribute("bt", bt); request.setAttribute("gn", gn); request.setAttribute("gj", gj); // 跳转 request.getRequestDispatcher("index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } public void init() throws ServletException { // Put your code here } }
说明: 这个类是出来由jsp页面发送过来的请求的。一个类只能出来一个请求
然后再web.xml中进行配置
<servlet> <servlet-name>ADDnews</servlet-name> <!-- 名字 --> <servlet-class>com.zhy.t502.entity.Addnews</servlet-class> <!-- 路径 --> </servlet> <servlet-mapping> <servlet-name>ADDnews</servlet-name> <!-- 名字 --> <url-pattern>/ADDnews</url-pattern> <!-- jsp页面的请求路径 --> </servlet-mapping>
在服务器启动时会最先读取web.xml配置文件
时间: 2024-10-09 06:30:27