JSP基礎,jsp基礎教程
JSP的出現
上篇文章中提到了用Servlet開發,渲染用戶端頁面是需要一句一句輸出的,這樣,編寫和修改HTML都會非常不方便。也就是說Servlet寫“業務層”很合適,但是寫“表現層”很繁瑣。
於是寫“表現層”很方便的JSP就誕生了。這裡需要指明,JSP並沒有增加任何本質上不能用Servlet實現的功能。但是,在JSP中編寫靜態HTML更加方便,不必再用println語句來輸出每一行HTML代碼。更重要的是,它將業務層與表現層分離開來,使得編碼更方便和易維護。
JSP與Servlet
書寫方便只是SUN為我們編碼所提供的一種服務。有了JSP也不能忘了Servlet,因為後者正是JSP的本質。JSP可以說是對Servlet更高層次的一種封裝。請求JSP時,經過轉化,褪去封裝後,就可以看到它的真是面目還是Servlet,渲染頁面還是一行一行的輸出HTML代碼。
JSP的使用
現在一般我們寫的JSP就是將Java(Servlet)代碼嵌入到了HTML頁中,也就是將“業務層”與“表現層”混到了一起,與ASP的字碼頁非常相像,如下面一個ASP字碼頁:
<html><head><%Response.Write "<TITLE>"&my_name&"</TITLE>"%></head><%if my_domains<>"" thenif Request.ServerVariables("SERVER_NAME")<>my_url Then response.redirect "url.asp"End ifIf rikeenet("a")<>"" And IsNumeric(rikeenet("a")) Then Response.redirect "shop/1/myshop.asp?shopid="&rikeenet("a")&""Response.EndEnd If%><body class="<%= PageStyle %>"><!--#include file="FHeadAll.asp"--><div class="HackBox"></div><div id="Content" class="L250"><a name="main"></a><%ShowIndexNewProduct(9)shopuser()%></div></body></html>
我們可以看到,伺服器代碼與HTML代碼合在一起,非常混亂。JSP也很類似。也就是說由Java代碼與HTML代碼合在一起的JSP是很混亂的。
ASP.NET的出現彌補了ASP的這個缺點,它將顯示(aspx)與業務(aspx.cs)完全分離。當然根據SUN自己的推薦,JSP中應該僅僅存放與“顯示層”有關的東西,也就是說,只放輸出HTML網頁的部分。而所有的資料計算,資料分析,資料庫連接處理等,統統是屬於“業務層”,應該放在Java BEANS中。通過JSP調用Java BEANS,實現兩層的整合。
由上可知JSP的本質就是Servlet,執行時先由JSP引擎將它轉化為Servlet,然後再編譯為.class檔案。而且JSP類似Servlet也是一次編譯多次執行的。在第一次請求時才需要將它進行轉化和編譯。
HelloWorld----執行個體:
JSP頁----HelloWorld.jsp
<html><head><title> HelloWorld </title></head><body><%out.println("HelloWorld");%></body></html>
經JSP引擎轉化後的頁----HelloWorld_jsp.java
package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<html>\r\n"); out.write("\t<head>\r\n"); out.write("\t\t<title> HelloWorld </title>\r\n"); out.write("\t</head>\r\n"); out.write("\t<body>\r\n"); out.write("\t\t");out.println("HelloWorld"); out.write("\r\n"); out.write("\t</body>\r\n"); out.write("</html>"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } }}
執行步驟:
總結
感覺JSP是為了方便編碼與維護而對Servlet的一種高層次的封裝,它由HTML代碼和嵌入其中的Java代碼所組成,它的請求—響應(伺服器解析)過程與ASP/.NET大同小異,可參考如下文章:
http://blog.csdn.net/wang379275614/article/details/9771659
jsp入門問題
我寫了四個jsp檔案
1,user.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<form name="form" action="do.jsp">
請輸入使用者名稱:<input type="text" name="username" value="">
<input type=submit name="submit" value="提交">
</form>
</body>
</html>
2. do.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<%
String user=request.getParameter("username");
%>
<%
if(user.equals(""))
{%>
<jsp:forward page="/false.jsp"></jsp:forward>
<%}else{%>
<jsp:forward page="/success.jsp">
<jsp:param name="username" value="<%=user %>"/>
</jsp:forward>
<%} %>
</body
3.false.jsp
<%@ page language="java&......餘下全文>>
JSP入門詳解,急
jsp 最後還是被編成 servlet 在空器中啟動並執行
所以 要想把jsp 學好 首先得把servlet 搞明白
我一開始以為 直接學jsp就好了 可是後來發現有很多東西沒理解不好
我建議你 首先把servlet 搞清楚 繼承什麼類 以及servlet 的生命週期
在容器(如TOMCAT)中如何配置 ...
當然在實際項目中 servlet 常常被用作控制層來使用. 原理你必須掌握
把servlet搞清楚以後 你再回頭來學jsp 你會豁然開朗的 呵呵..