大纲:一、servlet类
二、web.xml
=====servlet类==========================================
package com.smud.servlet;import java.io.IOException;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.smud.dao.MapDAO;import com.smud.dao.impl.MapDAOImpl;import com.smud.model.Map;public class MapListServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //查询所有的地图信息 MapDAO dao = new MapDAOImpl(); List
注:
一、我们写的servlet类实际上是继承了HttpServlet ,所以感觉是可以单独作为一个web页面的形式存在。二、request.getRequestDispatcher(url).forward(request,response)是直接将请求转发到指定URL,所以该请求能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建。
=====web.xml==========================================
MapAddServlet com.smud.servlet.MapAddServlet MapAddServlet /map/mapAddServlet MapListServlet com.smud.servlet.MapListServlet MapListServlet /map/mapListServlet
注:<servlet-class>是指servlet类的位置
<url-pattern>是指servlet类的url地址
===附:jsp“模板”MapLib.jsp文件=====================================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="java.util.*" %><%@ page import="com.sss.model.Map" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>My JSP 'MapLib.jsp' starting page