Servlet 的职责 1. 获得前台的数据 2. 处理前台数据 3. 对数据包做出转发 Forward 转发:把该 servlet 中的 req 和 resp 中的数据包一起打包发送出去 只有一个请求 Redirect 重定向:不能把元 se
Servlet的职责
1. 获得前台的数据
2. 处理前台数据
3. 对数据包做出转发
Forward
转发:把该servlet中的req和resp中的数据包一起打包发送出去
只有一个请求
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
为依此次序