前言:以前寫了幾篇JSP的博客,如今又回過頭來看Servlet,溫故而知新,再回顧回顧,總會有收穫的。之前學習Servlet感受內容不少,如今看的時候,其實也沒多少東西,只需知道Servlet的生命週期,Servlet的實現方式,ServletContext做用域,接收和響應,轉發和重定向,我以爲差很少夠用了。固然,要是細究的話也不止這些,我針對的是新手。
轉發與重定向簡html
package servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.www.seocelve.comhttp.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//經過轉發帶數據
req.setAttribute("name","張三");
req.getRequestDispatcher("/send.jsp").forward(req,resp);
import javax.servlet.ServletContext;
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("/login"www.leyouzaixan.cn)
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//經過重定向帶數據
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("name","王二麻子");
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
<form action="CountServlet" method="post">
<h3>加法計算器</h3>
加數1:<input type=www.yisheng3yuL.com"number" name="one">
加數2:<input type="number" name=www.zbzxyL12.com"two">
<input type="submit" value="計算">
</form>
</body>
</html>java