JSP vs. JSF
Servlets and JSP (JavaServer Pages)
Original, widely-deployed standard
Used by google.com, ebay.com, walmart.com, and thousands of other popular sites
Low level by today’s standards
Covered in this tutorial
JSF (JavaServer Faces) Version 2
An official part of Java EE as of Java EE 6
runs in any recent Java-enabled server, including Tomcat
Higher-level features: integrated Ajax support, field validation, page templating, rich third-party component libraries such as PrimeFaces, etc. Designed around the MVC architecture.
Recommended for almost all new projects
Covered later
JSP vs. JSF: When to Use Which
Servlets and JSP
For maintaining and extending existing legacy projects
Servlets only
For apps with front ends that do not use a server-side framework
E.g., HTML with jQuery and jQuery UI
Servlets primarily handle the Ajax requests from jQuery and do not build full pages
JSF 2
For almost all new projects that involve dynamic pages
Usually combined with a rich component toolkit such as PrimeFaces
See http://www.coreservlets.com/JSF-Tutorial/primefaces
Technologies Used Internally with JSF
What Servlets and JSP are All About
Servlet:
read explicit date sent by client(from data)
read implicit data sent by client(request headers)
Generate the results
Send the explicit data back to client (HTML)
Send the implicit data to client(status codes and response headers)
简单说就是实现了数据传输
这样就使的动态网页成为可能
Mainstream
最流行的是java,Google报告说超过650 million网页用JSP
Simple Servlets
@WebServlet("/test1")
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println
("<!DOCTYPE html>\n" +
"<html>\n" +
"<head><title>A Test Servlet</title></head>\n" +
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1>Test</h1>\n" +
"<p>Simple servlet for testing.</p>\n" +
"</body></html>");
}
}
Using Helper Classes ##针对书写规范
所有的java代码都要写在一个包里
要坚持OOP原则
Generating the Server Response: HTTP Response Headers
response.setHeader(String headerName,String headerValue)//rename header
response.setDateHeader(String name, long millisecs)
.有一些可以定义HTTP传输的 语句
Advanced Topics
初始化:确保servlet最先加载,防止加载前有请求
service:dont touch it
why: service method does other things besides just calling doGet
,
To be continued
本文对比了JSP和JSF两种Java Web开发技术。JSP作为早期标准被广泛使用,适用于维护现有项目及简单的非框架应用。而JSF则为Java EE的一部分,拥有更高级特性如Ajax支持、字段验证等,推荐用于大多数新项目。
46万+

被折叠的 条评论
为什么被折叠?



