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,本身总结的流程图。
服务器