B/S开发模式html
1:从浏览器发出给服务器的数据包为请求为(Request)
2:从服务器返回给浏览器结果称为响应 为(Response)
3:Get方式是将请求信息写在地址栏上
4;Post是将数据存放在‘请求体’中隐形向服务器发送
复制代码
1:建立Servlet类,(建立后的项目结构)java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class FirstServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//用request,给服务器发出请求
String name = req.getParameter("name");
String html="<h1 style='color:red'>hi"+name+"!</h1><hr/>";
System.out.println("运行结果是==================="+html);
//服务器放回给浏览器的结果用response
PrintWriter out = resp.getWriter();
out.print(html);
}
}
复制代码
3:配置Web.xml文件,(这是一种映射机制)web
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--是对servlet的配置-->
<servlet>
<!--是对项目名的从新取名-->
<servlet-name>first</servlet-name>
<servlet-class>com.imooc.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--这里的servlet-name要和上面的servlet-name名字要相同-->
<servlet-name>first</servlet-name>
<!--绑定URL-->
<url-pattern>/hi</url-pattern>
</servlet-mapping>
</web-app>
复制代码
http://IP地址端口号/工程名/Url-mapping浏览器
如上图,我所建立工程访问方式为 http://localhost:8080/FirstServlet/hibash
接受单个参数:request.getParameter();
接受多个同名参数:request.getParamenterVlalues();
复制代码
get和post处理方法服务器
1,所用请求是Servlet 2,get请求doGet方法 3,post请求doPost方法app
1,装在web.xml 2建立构造函数 3初始化init() 4提供服务service()5 销毁destory()ide
在servlet3.x之后引入注解“Annotation”特性
注解有利于简化web.xml配置
核心注解@WebServlet
复制代码
在web.xml配置文件中加载<load-on-startup>
<load-on-startup>0~999</load-on-startup>//阿拉伯数字越小加载优先级越高
启动时候加载至关于系统中预处理复制代码