Http服务和JSP
需要先安装tomocat8.0,并且使用的IDEA
一个web项目
- 新建项目
- 写代码
// 新建一个class
@WebServlet("/test")
public class Main extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
String text = request.getParameter("text");
System.out.println("结果已经传入后台:" + text);
String output = "后台返回的结果加上前台的结果" + text;
response.getWriter().write(output);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把post转给get
doGet(request, response);
}
}
- 需要配置tomocat【查看工具/IDEA笔记】
- 运行后浏览器查看是不是执行成功
jsp,el,jstl
- jsp就是服务器渲染模板,他会被编译成html文件
- el就是存在于一个请求的数据,一个jsp能携带四个作用域的数据,作用域里的所有数据都能被el取出来后显示到jsp里
- jstl,当el里的数据要被规则的显示时,可以用订制的循环方式等等进行渲染
ArrayList<UserBean> users=new ArrayList<UserBean>();
UserBean ub1=new UserBean("zhao",20);
UserBean ub2=new UserBean("qian",40);
UserBean ub3=new UserBean("sun",60);
UserBean ub4=new UserBean("li",80);
users.add(ub1);
users.add(ub2);
users.add(ub3);
users.add(ub4);
ModelAndView mv = new ModelAndView("app/index");
mv.addObject("users",users);
return mv;
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>