@WebServlet(value = "*.category")
public class CategoryServlet extends HttpServlet {
CategoryService categoryService = new CategoryServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("html/text");
String path = req.getServletPath();
switch (path) {
case "/selectByPage.category":
selectByPage(req, resp);
break;
}
}
//分页查询
protected void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object object=req.getSession().getAttribute("manager");
if (object == null) {
resp.getWriter().print("null");
return;
}
String pageNo = req.getParameter("pageNo");
String likeValue = req.getParameter("likeValue");
PageModel pageModel = new PageModel();
pageModel.setPageNo(Integer.parseInt(pageNo));
pageModel.setLikeValue(likeValue);
List<Category> categories = categoryService.selectByPage(pageModel);
pageModel.setModelList(categories);
String jsonStr = JSONObject.toJSONString(pageModel);
resp.getWriter().print(jsonStr);
}
}
代码解析:
以上代码是一个基于Java Servlet的简单示例,它展示了如何使用Servlet处理HTTP请求,并根据请求路径(path)执行相应的业务逻辑。让我逐步解释这段代码的功能和结构。
@WebServlet注解:
java
@WebServlet(value = "*.category")
public class CategoryServlet extends HttpServlet {
// Class implementation
}
@WebServlet(value = "*.category") 注解指定了Servlet的映射路径,表示该Servlet会处理所有以 .category 结尾的URL请求。
CategoryServlet类:
继承自HttpServlet类,因此可以处理HTTP请求。
CategoryService初始化:
java
CategoryService categoryService = new CategoryServiceImpl();
CategoryService接口通过CategoryServiceImpl类实现,用于处理业务逻辑。
doGet和doPost方法:
doGet方法直接调用doPost方法,确保处理GET请求和POST请求的逻辑一致。
doPost方法:
java
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("html/text");
String path = req.getServletPath();
switch (path) {
case "/selectByPage.category":
selectByPage(req, resp);
break;
// Add more cases for other paths if needed
}
}
设置请求和响应的编码和内容类型为UTF-8。
通过req.getServletPath()获取请求的Servlet路径,然后使用switch语句根据路径执行相应的方法。
selectByPage方法:
protected void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object object = req.getSession().getAttribute("manager");
if (object == null) {
resp.getWriter().print("null");
return;
}
String pageNo = req.getParameter("pageNo");
String likeValue = req.getParameter("likeValue");
PageModel pageModel = new PageModel();
pageModel.setPageNo(Integer.parseInt(pageNo));
pageModel.setLikeValue(likeValue);
List<Category> categories = categoryService.selectByPage(pageModel);
pageModel.setModelList(categories);
String jsonStr = JSONObject.toJSONString(pageModel);
resp.getWriter().print(jsonStr);
}
检查当前会话中是否存在名为"manager"的属性,如果不存在则返回字符串"null"。
从请求参数中获取pageNo(页码)和likeValue(模糊查询的条件值)。
创建一个PageModel对象,并设置页码和模糊查询条件。
调用categoryService的selectByPage方法,根据pageModel进行分页查询,获取Category对象的列表。
将查询结果设置回pageModel中。
使用JSONObject.toJSONString()方法将pageModel对象转换为JSON字符串。
将JSON字符串作为响应内容返回给客户端。
总结:以上代码演示了一个简单的Servlet类,根据不同的请求路径执行不同的业务逻辑(这里主要是分页查询),并将结果以JSON格式返回给客户端。这种方式适用于基于Java的Web应用程序中,用于处理前端页面的数据请求和响应。
以上代码是一个使用jQuery编写的JavaScript函数,主要实现了以下功能:
页面加载完成后执行函数:
javascript
$(function () { ... });
这段代码使用了jQuery的语法,表示在页面DOM加载完成后执行包裹的函数。
发起POST请求:
javascript
var url = "/fictionmanager/queryFullName.maneger";
$.post(url, function (data) { ... });
使用jQuery的$.post()方法向服务器发起POST请求,请求的URL为/fictionmanager/queryFullName.maneger。
处理服务器响应:
javascript
function (data) {
if (data == "null") {
window.location.href = "./login.html";
} else {
alert(data);
if (data != null && data != "" && data != "null") {
var jsonObj = JSON.parse(data);
var fulltime = jsonObj.managerfullname;
$("fullname").html(fulltime);
} else {
window.location.href = "./login.html";
}
}
}
当服务器返回数据后,会执行这个回调函数。首先检查data的值:
如果data等于字符串"null",则将页面重定向到./login.html,即跳转到登录页面。
否则,弹出一个包含服务器返回数据的警告框(用于调试或显示返回的数据),然后进一步检查data:
如果data不为null、空字符串和"null",则将data解析为JSON对象,并获取其中managerfullname字段的值,然后将这个值设置为页面中<fullname>元素的HTML内容。
如果data为空、为null或者为字符串"null",同样将页面重定向到./login.html,即跳转到登录页面。
总结:以上代码的主要目的是向服务器发起POST请求获取数据,根据返回的数据决定页面的跳转或者更新页面内容,通常用于页面初始化时进行用户登录状态的验证或者信息的展示。
知识点:
JSON(JavaScript Object Notation)
对象是一种轻量级的数据交换格式,它易于人类阅读和编写,同时也易于机器解析和生成。在JavaScript中,JSON对象是一种文本格式,用于表示结构化数据。它由键值对组成,键名使用双引号括起来,值可以是字符串、数字、布尔值、数组、对象或者null。
JSON对象的基本语法如下:
json
{
"key1": "value1",
"key2": 123,
"key3": true,
"key4": ["apple", "banana", "cherry"],
"key5": {
"nestedKey1": "nestedValue1",
"nestedKey2": 456
},
"key6": null
}
在JavaScript中,可以使用JSON.parse()方法将JSON格式的字符串转换为JavaScript对象,例如:
javascript
var jsonString = '{"name": "John", "age": 30, "city": "New York"}';
var jsonObject = JSON.parse(jsonString);
console.log(jsonObject.name); // 输出: John
console.log(jsonObject.age); // 输出: 30
console.log(jsonObject.city); // 输出: New York
反之,可以使用JSON.stringify()方法将JavaScript对象转换为JSON格式的字符串,例如:
javascript
var person = {name: "John", age: 30, city: "New York"};
var jsonString = JSON.stringify(person);
console.log(jsonString); // 输出: {"name":"John","age":30,"city":"New York"}
JSON在网络数据交换和前后端数据通信中广泛应用,因为它简单、通用且易于解析。