servlet技术简介
Servlet技术:
在个人认为,是进行两个不同板块交互所使用的东西,更深入一步是,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.
- 首先我们先来看看Jsp技术,Jsp技术是以servlet技术为基础的。
Jsp全称:java server page,顾名思义,位于服务器端的程序。
另外,Jsp和servlet之间的关系需要理解一下:
- client 发送用户请求给server,http请求数据。
- web服务器接受请求并解析分配给Jsp
引擎
,一般通过url(localhost:8080/index.jsp)来请求jsp文件。 - Jsp引擎调用针对的Jsp文件,不仅将其转化为servlet类并将其Jsp元素转化为Java代码
这种转化只是简单地将所有模板文本改用 println() 语句,并且将所有的 JSP 元素转化成 Java 代码。
- Jsp引擎转换这些java代码为servlet类,同时将请求数据传递给Servlet引擎。
- 等待数据传递到servlet引擎中,web服务器其中的某个组件调用servlet引擎并运行servlet类。
- servlet类之后产生html格式的输出流并嵌套于response中返回用户的浏览器。
我在这里意识到了一个知识点盲区:网络请求和回应的流程,通过在网上搜寻资料,看到一篇大佬的文章。
[Servlet基础](https://www.cnblogs.com/rocomp/p/4808924.html) ![servlet网络中运行流程](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528213933314&di=488341b49063a8117810d751b3ce94f2&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D1076809686%2C1087400012%26fm%3D214%26gp%3D0.jpg) ![形象生动展示过程](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528214025603&di=7dc3af8f2bb5e1ba2eadb63c807d933c&imgtype=0&src=http%3A%2F%2Fs2.sinaimg.cn%2Fmw690%2F003AIxwozy709yO4Q1311) 写了这么多,我其实也有点乱,接下来用一个”HelloWorld”来整理下思路(表单元素呈现)
只有前段存在表单的时候才会存在交互,才能调用超类Httpservlet中的service()方法
- index.jsp
<%@ 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>
- Dao操作类 (servlet类)
HelloWorld类
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的敏感度)
}
}
- web.xml文件
<?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,根目录不需要配置。*
目录结构非常重要!!!!!!!!
运行啥的不多说了。。
In conclusion1,自己总结的流程图。