以前不管是servlet还struts,都少不了xml配置文件,现在servlet先进到什么程度,我认为很接近springMVC了。最新版的servlet是6.1,这可以从tomcat的官网上获取Apache Tomcat® - Which Version Do I Want?https://tomcat.apache.org/whichversion.html新版的servlet很简洁,没有过多的配置文件,@WebServlet(name="ApiArticleServlet", urlPatterns="/api/article/*")是核心,定义了访问路径,同时一般用到的方法有doGet和doPost
doGet 我一般是用来做页面跳转doPost一般是用来做方法,以下是一个简单的servlet例子,
在doGet方法中,request.getRequestDispatcher很重要,用来跳转比如文章的列表页则可以articleList.jsp,String p0 = mapping.getMatchValue();表示*号后面的参数
同样在doPost方法中String p0 = mapping.getMatchValue();表示*号后面的参数,可以用这个区分不同的方法,比如list,update,del,save。
当然servlet本身还自带一个doPut,doDelete等不常用的方法,一般都是结合doPost加上String p0 = mapping.getMatchValue();来代替就够了。
package net.mbzj.servlet.api;
import java.io.IOException;
import java.util.List;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.mbzj.bean.Category;
import net.mbzj.bean.Title;
import net.mbzj.dao.ArticleDao;
import net.mbzj.dao.CategoryDao;
import net.mbzj.utils.AjaxUtils;
import net.mbzj.utils.Criteria;
import net.mbzj.utils.StringUtils;
@WebServlet(name="ApiArticleServlet", urlPatterns="/api/article/*")
public class ArticleServlet extends HttpServlet{
/**
* 文章接口
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
HttpServletMapping mapping = request.getHttpServletMapping();
String p0 = mapping.getMatchValue();
try {
}catch(Exception e) {
e.printStackTrace();
}finally {
//映射jsp文件
request.getRequestDispatcher("/WEB-INF/jsp/"+s+".jsp").forward(request, response);
}
}
public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
HttpServletMapping mapping = request.getHttpServletMapping();
String p0 = mapping.getMatchValue();
JSONObject jo = new JSONObject();
try {
if(StringUtils.isNotEmpty(p0)&&p0.equals("list")) {
String page = request.getParameter("page");
String limit = request.getParameter("limit");
String title = request.getParameter("title");
String shortTitle = request.getParameter("shortTitle");
String categoryId = request.getParameter("categoryId");
String attr = request.getParameter("attr");
String status = request.getParameter("status");
Category category = CategoryDao.findById(Long.valueOf(categoryId));
Criteria cri = new Criteria();
if(page!=null) {
cri.setPageNo(Integer.valueOf(page));
}else {
cri.setPageNo(Integer.valueOf(1));
}
if(limit!=null) {
cri.setPageSize(Integer.valueOf(limit));
}else {
cri.setPageSize(10);
}
if(StringUtils.isNotEmpty(title)) {
cri.like("title", title);
}
if(StringUtils.isNotEmpty(shortTitle)) {
cri.like("shortTitle", shortTitle);
}
if(StringUtils.isNotEmpty(categoryId)) {
cri.eq("categoryId", categoryId);
}
if(StringUtils.isNotEmpty(attr)) {
cri.beforeLike("attr", attr);
}
if(StringUtils.isNotEmpty(status)) {
cri.eq("status", status);
}
cri.eq("siteId", category.getSiteId());
cri.setOrderBy("createTime desc");
//文章列表不查text字段
List<Title> list = ArticleDao.page(cri);
jo.put("code", 0);
jo.put("data", list);
jo.put("count",ArticleDao.countTotal(cri));
}
}catch(Exception e) {
jo.put("code",-1);
jo.put("msg", StringUtils.MSG_ERROR);
e.printStackTrace();
}finally {
AjaxUtils.printJson(response, jo.toJSONString());
}
}
}
以下是文件详情的例子,可以有很好的Reset风格,其中URL:/article/view/231 ,
231就是对应String p0 = mapping.getMatchValue();拿到的值