背景:
ecplise+tomcat+servlet
我们在学习servlet时候,因为前面基础没学好,导致servlet看不懂,比如笔者当时就是servlet的web.xml看不懂,如果我们能先看懂web.xml,知道前端访问哪个接口,调用哪个servlet,至少就能从宏观上了解项目,对项目有一定促进作用。本文的目标在于使新手尽量简单看懂前端请求和web.xml中的配置有啥关系。
整体项目介绍链接如下:
Servlet技术实现服务端,Android平台作为客户端,实现一个个人店铺_zew1040994588的博客-CSDN博客
登录例子讲解:
项目整体目录:
后端登录接口demo:
这里我们不关注具体代码内容咋写的,只关注他和web.xml有什么关系!
这里是userLoign.java里面的内容:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.User;
import dao.UserDao;
import net.sf.json.JSONObject;
import utils.JsonObjectWeb;
import utils.JsonResultUtil;
public class userLogin extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//0、解决返回中文乱码问题
resp.setCharacterEncoding("UTF-8");
JSONObject web_dict = JsonObjectWeb.getJsonInfoDict(req);
String username = web_dict.getString("username");
String password = web_dict.getString("password");
User user = new User();//创建user对象
user.setUsername(username);
user.setPassword(password);
/*
* 这里需要查询数据库是否重名,若有重名则不允许插入,返回不同状态码
* */
int flag = 0;
UserDao dao = new UserDao();
User login_user = null;
try {
login_user = dao.login(username,password);
if(login_user != null) {
int user_id = login_user.id.intValue();
JSONObject data_dict = new JSONObject();
data_dict.put("user_id", user_id);
JSONObject json = JsonResultUtil.getJson(200,data_dict, "登录成功");
PrintWriter writer = resp.getWriter();
//这里是返回json
writer.print(json);
flag = -1;
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (flag == 0) {
JSONObject json = JsonResultUtil.getJson(1002, "登录失败");
PrintWriter writer = resp.getWriter();
//这里是返回json
writer.print(json);
}
}
}
后端web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>shop_sys2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>user_login</servlet-name>
<servlet-class>servlet.userLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user_login</servlet-name>
<!-- 登录 -->
<url-pattern>/user/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>user_register</servlet-name>
<servlet-class>servlet.userRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user_register</servlet-name>
<!-- 注册 -->
<url-pattern>/user/register</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>user_update_password</servlet-name>
<servlet-class>servlet.userChange</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user_update_password</servlet-name>
<!-- 注册 -->
<url-pattern>/user/update/password</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>product_view_all</servlet-name>
<servlet-class>servlet.productView</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>product_view_all</servlet-name>
<!-- 展示所有商品 -->
<url-pattern>/product/view/all</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cart_view</servlet-name>
<servlet-class>servlet.cartView</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cart_view</servlet-name>
<!-- 显示用户购物车 -->
<url-pattern>/cart/view</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>product_add_cart</servlet-name>
<servlet-class>servlet.cartAdd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>product_add_cart</servlet-name>
<!-- 添加商品到购物车 -->
<url-pattern>/product/add/cart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>product_delete_cart</servlet-name>
<servlet-class>servlet.cartDelete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>product_delete_cart</servlet-name>
<!-- 将商品从购物车中删除 -->
<url-pattern>/product/delete/cart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cart_settlement</servlet-name>
<servlet-class>servlet.cartSettlement</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cart_settlement</servlet-name>
<!-- 购物车页面对商品进行结算 -->
<url-pattern>/cart/settlement</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>view_orders</servlet-name>
<servlet-class>servlet.orderView</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>view_orders</servlet-name>
<!-- 查看所有订单 -->
<url-pattern>/view/orders</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>product_purchase</servlet-name>
<servlet-class>servlet.productPurchase</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>product_purchase</servlet-name>
<!-- 商品页面进行直接购买 -->
<url-pattern>/product/purchase</url-pattern>
</servlet-mapping>
</web-app>
前端登录页面:
前端界面调用后端的接口:
接口名为:127.0.0.1:8080/shop_sys2/user/login
逻辑讲解:
1-先配置 <servlet></servlet>里面的内容,再配置<servlet-mapping></servlet-mapping>里面的内容。
2-<servlet></servlet>这个配置相当于是绑定userLogin.java,
而在userLogin.java中我们写的void doPost(省略形参)具体是后端预备用来处理前端以Post方式请求127.0.0.1:8080/shop_sys2/user/login时所编写的后台逻辑,比如查询数据库看看用户能否登录之类的【当然也可以处理Get请求,我们没写处理Get的】。而怎么把这个servlet让前端能够访问的到,这就是web.xml的作用所在了。当然有很多种处理方式,web.xml是最基础的一种。后期熟练可以用注解。
3- <servlet-mapping> </servlet-mapping>里面的内容配置
就是应对前端请求127.0.0.1:8080/shop_sys2/user/login的Post方式,
3.1-通过<servlet-mapping></servlet-mapping>中的<url-pattern>/user/login</url-pattern>让后台识别到前端发送了127.0.0.1:8080/shop_sys2/user/login这个请求,
此后根据<servlet-name>user_login</servlet-name>获取到的user_login,去找到 <servlet></servlet>内容中的<servlet-name>user_login</servlet-name>,
让逻辑能够进入到对应的servlet去处理【这一步是由<servlet-class>servlet.userLogin</servlet-class>里面的内容servlet.userLogin来决定的!!!!】,再之后就是具体的userLogin.java的void doPost(省略形参)里面的查询数据库之类的了。
这里文字叙述若觉得不理解,可以看看下面的图,兴许你就理解了。
<servlet>
<servlet-name>user_login</servlet-name>
<servlet-class>servlet.userLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user_login</servlet-name>
<!-- 登录 -->
<url-pattern>/user/login</url-pattern>
</servlet-mapping>