javaweb之JSP+Servlet

javaweb之JSP+Servlet

When using java web development projects, it can be used in the back end Servlet, use front-end JSP, to introduce the following with the use of both.

  • Servlet

Servlet concept is actually very simple, is essentially a Servlet developed java class compliance. Running on a Web server or application server, which is a database or between applications on the server and HTTP request from the Web browser or other HTTP clients intermediate layer.

Servlet life cycle

Pictures from https://www.runoob.com

  • JSP

JSP (full name of the Java Server Pages) is advocated by Sun Microsystems, and many of the companies involved in the creation of a common software developers can respond to client requests, and dynamically generate HTML, XML or other format of the document technical standard Web pages . JSP technology is based on Java language as a scripting language, JSP pages it provides an interface to serve for the entire HTTP application server-side Java library unit.

Features: Html can be mixed and java code!

In fact, JSP is also a Servlet, JSP is compiled after a Servlet class files. Usually pay more attention to display JSP pages and Servlet focus more on logic control.

img

A complete network requests:

  • Like other ordinary web as your browser sends an HTTP request to the server.
  • Web server recognizes this as a request for a JSP page, and passes the request to the JSP engine. It is accomplished by using a URL or .jsp file.
  • JSP JSP engine load files from the disk, and then convert them into Servlet. This transformation will simply switch all template text println () statements, and all JSP elements converted into Java code.
  • JSP engine Servlet compiled into an executable class, and the original request is passed to the Servlet engine.
  • A component of the Web server will call the Servlet engine, and then load and execute the Servlet class. During execution, Servlet to generate output in HTML format and embedded it in the HTTP response to the Web server.
  • Web server in the form of static HTML pages of the HTTP response back to your browser.
  • Eventually, Web browsers handle HTTP response in dynamically generated HTML pages, as if in the process the same as static pages.

Here is a practical application of project:

A Servlet:

//GetAllLesson
package intergration.controller.lesson;


import intergration.Service.LessonService;
import intergration.entity.Lesson;
import org.jdom.JDOMException;
import org.xml.sax.SAXException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/lesson/getAllLesson")  //访问Servlet的url
public class GetAllLesson extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        LessonService lessonService = new LessonService();

        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<Lesson> lessonList = null;
        try {
            lessonList = lessonService.getAllLesson();  //从后台得到数据
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        modelMap.put("lesson", lessonList);
        if(lessonList == null){
            modelMap.put("success", false);
        }else {
            modelMap.put("success", true);
        }

        req.setAttribute("lesson_list",lessonList);  //将数据传回前端
        req.getRequestDispatcher("getAllLesson.jsp").forward(req, resp); //重定向

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
        doGet(req, resp);
    }
}

Corresponding getAllLesson.jsp

<%@ page import="java.util.List"%>
<%@ page import="intergration.entity.Lesson" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/11/27
  Time: 17:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>getAllLesson</title>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/public.css" />
</head>
<body>

<div id = "left_table">
    <form action="getLessonById" method="post">
        <input type="text"  name="id" value="" placeholder="课程号" />
        <button type ="submit">查询</button>
    </form>
</div>
<div id = "table" >
    <div class="add">
        <form action="insertLesson" method="post">
            <input type="text"  name="id" value="" placeholder="ID" />
            <input type="text"  name="name" value="" placeholder="课程名" />
            <input type="text"  name="sex" value="" placeholder="教师" />
            <input type="text"  name="class" value="" placeholder="学时" />
            <select name = "database">
                <option value = "test1">test1</option>
                <option value = "test2">test2</option>
            </select>
            <button type ="submit">新增</button>
        </form>
    </div>

    <table cellpadding="0" cellspacing="0">
        <thead>
        <tr>
            <th>ID</th>
            <th>课程名</th>
            <th>教师</th>
            <th>学时</th>
        </tr>
        </thead>
        <%
            //获取数据
            List<Lesson> lesson_list = (List<Lesson>)request.getAttribute("lesson_list");
        %>

        <%
            for(Lesson lesson:lesson_list){
        %>
        <tbody>
            <tr align="center">
                <td> <%= lesson.getLessonId() %></td>
                <td> <%= lesson.getLessonName() %></td>
                <td> <%= lesson.getTeacherName() %></td>
                <td> <%= lesson.getHours() %></td>

                <td  width="20%">
                    <span class="delete" ><a href="deleteLessonById?id=<%= lesson.getLessonId()%>">删除</a> </span>
                    <span class="edit" onclick="dis( <%= lesson.getLessonId() %>,'<%=lesson.getLessonName() %>',<%= lesson.getTeacherName() %>, <%= lesson.getHours() %>);">编辑</span>

                </td>
            </tr>
        </tbody>
        <%
            }
        %>

    </table>
    <div id="mask">
        <div class="mask">
            <div class="title">
                编辑
                <span onclick = "nodis()">
                            X
                        </span>
            </div>
            <div class="content">
                <form action="updateLesson" method="post">
                    <input type="text"  name="id" id = "update_id"value="" placeholder="ID" />
                    <input type="text"  name="name" id = "update_name" value="" placeholder="课程名" />
                    <input type="text"  name="sex" id = "update_sex"value="" placeholder="教师" />
                    <input type="text"  name="class" id = "update_class"value="" placeholder="学时" />
                    <button type ="submit">更新</button>
                </form>
            </div>
        </div>
    </div>


</div>

</body>

<script>
    function dis(Id,Name,Sex,Class) {
        event.stopPropagation();
        document.getElementById("mask").style.display = "block";
        document.getElementById("update_id").setAttribute("placeholder",Id);
        document.getElementById("update_name").setAttribute("placeholder",Name);
        document.getElementById("update_sex").setAttribute("placeholder",Sex);
        document.getElementById("update_class").setAttribute("placeholder",Class);
        document.getElementById("update_id").setAttribute("value",Id);
        document.getElementById("update_name").setAttribute("value",Name);
        document.getElementById("update_sex").setAttribute("value",Sex);
        document.getElementById("update_class").setAttribute("value",Class);
    }
    function nodis() {
        event.stopPropagation();
        document.getElementById("mask").style.display = "none";
    }
</script>

</html>

Guess you like

Origin www.cnblogs.com/lyc1226/p/12142640.html