• servlet&jsp


    Servlet的职责

    1. 获得前台的数据

    2. 处理前台数据

    3. 对数据包做出转发

    Forward

    转发:把该servlet中的reqresp中的数据包一起打包发送出去

    只有一个请求

    Redirect

    重定向:不能把元servlet中的数据包发送出去

    转发和重定向的区别

    package cn.jiedada.login;
    
    import java.io.IOException;
    
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    @WebServlet("/login")
    public class Login extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name = req.getParameter("name");
            String pwd = req.getParameter("pwd");
            ServletContext context = req.getServletContext();
            System.out.println(context.getContextPath());
            System.out.println(context.getRealPath("/"));
            if(name.equals("jieshuai")&&pwd.equals("123456")){
                HttpSession session = req.getSession();
                session.setAttribute("name", name);
                resp.sendRedirect("success.jsp");
            }else {
                req.setAttribute("error", "你的账号或者密码错误");
                req.getRequestDispatcher("index.jsp").forward(req, resp);
            }
        }
    }
    View Code

    JSP

    也是servlet但是用于发送页面请求

    获得application

    Req.getServerContext通过获得

    三个指令

    Page

    Include

    四个作用域

    pageContext

    Requret

    Session

    application

    九个内置对象

    Requret

    Response

    Out

    pageContext

    Session

    Application

    Confing

    Exciption

    page

    <%@ page pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <h1>欢迎登陆</h1>
        <%! int a=10;
            public void eat(){
                System.out.print("吃东西");
            }
        %>
        <%
            for(int i=0;i<5;i++){
                System.out.println(i);
            }
            int age=20;
        %>
        <%=age %>
    </body>
    </html>
    View Code
    <%@page import="java.util.Date"%>
    <%@ page pageEncoding="UTF-8" errorPage="error.jsp"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <!--  <%@ include file="index.html" %>
        <%=new Date() %> 
        -->
        <%
            int a=1/0;
        %>
    </body>
    </html>
    View Code
    <%@ page pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <form action="login" method="get">
                <h2>get请求<span><%=request.getAttribute("error") %></span></h2>
                账号:<input type="text" name="name"><br></br>
                密码:<input type="password" name="pwd"><br></br>
                <input type="submit" value="提交">
        </form>
    </body>
    </html>
    View Code
    <%@ page pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <form action="login" method="get">
                <h2>get请求<%=request.getAttribute("error") %></h2>
                账号:<input type="text" name="name"><br></br>
                密码:<input type="password" name="pwd"><br></br>
                <input type="submit" value="提交">
        </form>
    </body>
    </html>
    View Code
    <%@ page pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <form action="suanfa" method="get">
            <input type="text" name="a">
            <select name="fuhao">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" name="b">
            <input type="submit" value="=">
            <input type="text" value="<%=request.getAttribute("zhi") %>>">
        </form>
    </body>
    </html>
    View Code
    <%@ page pageEncoding="UTF-8"%>
    <!DOCTYPE html >
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
        <h1>欢迎<span><%=session.getAttribute("name") %></span></h1>
    </body>
    </html>
    View Code

    为依此次序

  • 相关阅读:
    Node.js安装及环境配置之Windows篇
    盘点.NET JIT在Release下由循环体优化所产生的不确定性Bug
    开源!一款功能强大的高性能二进制序列化器Bssom.Net
    开源 , KoobooJson一款高性能且轻量的JSON框架
    通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
    .Net Web开发技术栈
    web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。
    .Net高级进阶,教你如何构建企业模型数据拦截层,动态控制字段验证
    .Net 如何模拟会话级别的信号量,对http接口调用频率进行限制(有demo)
    .Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11397910.html
Copyright © 2020-2023  润新知