servlet技術簡介 css
在我的認爲,是進行兩個不一樣板塊交互所使用的東西,更深刻一步是,servlet
服務器
上的程序來處理交互功能的。舉例來講,
as you can see on the picture above,each item was separated by The Theory of MVC.the part explaining how server get the request from user where our servlet plays a vital role in transferring the package to the server. html
引擎
,通常經過url(localhost:8080/index.jsp)來請求jsp文件。這種轉化只是簡單地將全部模板文本改用 println() 語句,而且將全部的 JSP 元素轉化成 Java 代碼。
只有前段存在表單的時候纔會存在交互,才能調用超類Httpservlet中的service()方法
- index.jsp java
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body>
<form action="HelloWorld" method="post" target="_blank">
<input name="aa" type="text"><br>
<input name="bb" type="text"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
package com.Demo.Dao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorld() {
// TODO Auto-generated constructor stub
super();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//設置響應屬性
resp.setContentType("text/html;charset=UTF-8");
PrintWriter pw=resp.getWriter();
String title="響應頁面";
String docType = "<!DOCTYPE html> \n";
pw.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>第一項操做:</b>: "
+ req.getParameter("aa") + "\n" +
" <li><b>第二項操做:</b>: "
+ req.getParameter("bb") + "\n" +
"</ul>\n" +
"</body></html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
//在以前的時候,個人又一個知識點盲區(super和this的敏感度)
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.Demo.Dao.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
*很是強調一點:url-pattern是須要的注意
http://localhost:8080/servlet_Demo/HelloWorld
不能在這裏配置成
/servlet_Demo/HelloWorld,根目錄不須要配置。* web
運行啥的很少說了。。瀏覽器
In conclusion1,本身總結的流程圖。
服務器