The most entry-level servlet, front-end connection
Servlet development process for
- 1. The configuration file is required first, because the servlet needs to be initialized (otherwise the front end will not jump to the back end)
- 2. Write back-end according to business (front-end page) requirements
-
2.1写客户端请求页面
-
2.2写客户端响应页面
-
2.3写后台处理前端发过来的数据(登录页面login),处理完再转发到前端去(登录成功跳转到主页面index)
-
1. Write a configuration file
<?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>Day30</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>
<!--这里是去找一个名字叫reg的mapping(映射) -->
<servlet-name>reg</servlet-name>
<!--这里是需要由外部连接的java类的路径-->
<servlet-class>com.etime.Regiest</servlet-class>
</servlet>
<!--创建映射-->
<servlet-mapping>
<!--给这个映射命名为reg -->
<servlet-name>reg</servlet-name>
<!--这里写他要由哪个url路径来访问他 -->
<url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>
2. Write the front-end, write and explain the data passed into the back-end
1. Login page (html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body>
<h3>用户登录</h3>
<!--用绝对路径的方法来提交 /项目名/路径 -->
<form action="/Day30/reg" method="post">
<!--为了方便测试写的一个value默认值 -->
<!--这里的name属性很重要,后台需要靠这个来确定获取哪个文本框的值 -->
昵称:<input type="text" name="username" value="zhangsan"><br/>
密码:<input type="password" name="pwd" value="123456"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
2. Receive data page (jsp page)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title>
</head>
<body>
<!--el表达式,可以获取地址栏的信息用${
param.想要获取信息的字段名}-->
<h1>欢迎${
param.username}登录</h1><br/>
<h1>您的密码是:${
param.pwd}</h1><br/>
</body>
</html>
3. Write classes and process data in the background
package com.etime;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regiest extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 告知客户端使用哪种码表
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
// 使用request的getParameter方法获取到前端提交来的数据
//这里getParameter方法里的参数要和前端文本框里的name属性的值一一对应
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
// 使用response的sendRedirect方法来跳转页面(重定向)
// 一个全新的url,打开一个全新的页面
// (会乱码)
response.sendRedirect("/Day30/index.jsp?username=" + username + "&pwd=" + pwd);
// 相对的还有一个请求转发(forward)地址栏不变,站内转发
// 不会乱码
// RequestDispatcher requestDispatcher =
// request.getRequestDispatcher("/index.jsp?username="+username+"&pwd="+pwd);
// requestDispatcher.forward(request, response);
//request.setAttribute("stu", stu);
//request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
Run screenshot
This is request redirection:
This is request forwarding:
the address bar of these two methods is different.
Request forwarding is a new url like a server request.
Request redirection is based on the original request and automatically redirects the request to another page. , Without going through the server, the address bar remains unchanged and forwarded within the site.
end
So far is a very simple use of servlet to achieve front-end connection