1. 程式人生 > >servlet與jsp的分工協作

servlet與jsp的分工協作

servlet和jsp都是前段語言的一種,但是servlet一般用於與伺服器之間的互動,而jsp一般用於實現頁面內容的顯示

el表示式:

全程:expression language 用來在jsp用來展示結果的語言
語法:${ 表示式語言 }

登陸驗證的案例:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/9/19
  Time: 22:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/s1" method="post">
    <input type="text" name="username" placeholder="請輸入使用者名稱">
    <input type="password" name="password" placeholder="請輸入密碼">
    <input type="submit" value="登入">
</form>
</body>
</html>
package controller;

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 java.io.IOException;

@WebServlet(urlPatterns = "/s1")
public class servlet1 extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("username");
        String password = req.getParameter("password");
        if ("zhangsan".equals(name)&&"123".equals(password)){
            req.getRequestDispatcher("2.jsp").forward(req, resp);
        }else {
            req.getRequestDispatcher("1.jsp").forward(req, resp);
        }


    }
    }
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/9/19
  Time: 22:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*,controller1.shop8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
歡迎您,恭喜您登陸成功!
</body>
</html>

實現的登陸功能:

密碼如果正確則會進入成功的頁面:

 

反之,如果使用者名稱或者密碼不正確,則會返回到登陸的頁面:

jstl標籤庫:

全程:java standard tag library,主要的工具就是配合el表示式來更加便捷的實現jsp的頁面的現實功能,但是前提是要正確的加入jstl的jar包,並在jsp的頁面上加入程式碼

<%@ taglib uri="標籤庫唯一標識" prefix="字首" %>

常用的jstl的方法一般有c:forEach以及c:if方法,通過下文的案例來進行說明:

package controller1;

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/q1")
public class ServletTest extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> students = new ArrayList<>();
        Student student = new Student(1, "zhangsan", "男");
        Student student1 = new Student(2, "lisi", "女");
        Student student2 = new Student(3, "wangwu", "男");
        students.add(student);
        students.add(student1);
        students.add(student2);
        req.setAttribute("aaa",students);
        req.getRequestDispatcher("2.jsp").forward(req, resp);
    }
}
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/9/19
  Time: 22:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*,controller1.shop8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table border="1" width="100%">
    <tbody>
    <c:forEach items="${aaa}" var="d">
        <tr>
            <td><input type="text" name="sid" value="${d.id}"></td>
            <td><input type="text" name="sname" value="${d.name}"></td>
            <td><input type="text" name="ssex" value="${d.sex}"></td>
            <td><c:if test="${d.id>2}">編號大於2</c:if></td>
        </tr>
    </c:forEach>
    </tbody>
</table>
</body>
</html>

效果展示如下: