接下来几天我们将根据老师的讲解做出一个小型的例子,来深入了解JAVAWEB的相关内容,最后的完整项目会上传。
Request+Response
- 请求行
- 请求头部
- 空行
- 请求数据
Servlet
- DNS域名解析
- 建立连接(TCP/IP三次握手)
- demo
package com.pactrea.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class IndexServlet extends HttpServlet {
@Override
public void init(){
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response){
try {
response.sendRedirect("jsp/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
@Override
public void destroy(){
}
}
JavaWebDemo
package com.pactrea.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class IndexServlet extends HttpServlet {
@Override
public void init() {
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("name", "张三");
// 重定向 不带参数
// try {
// response.sendRedirect("jsp/index.jsp");
// } catch (IOException e) {
// e.printStackTrace();
// }
// 转发 带着信息转发
try {
request.getRequestDispatcher("jsp/index.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}
@Override
public void destroy() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>haha</servlet-name>
<servlet-class>com.pactrea.servlet.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>haha</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/19
Time: 13:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>jsp</title>
</head>
<body>
jsp包的index
<%=request.getAttribute("name")%>
${name}
</body>
</html>