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>

实现的登陆功能:

密码如果正确则会进入成功的页面:

反之,如果用户名或者密码不正确,则会返回到登陆的页面:

扫描二维码关注公众号,回复: 3907964 查看本文章

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>

效果展示如下:

猜你喜欢

转载自blog.csdn.net/weixin_42827269/article/details/82818529