[javaEE] jsp入門,javaeejsp入門

來源:互聯網
上載者:User

[javaEE] jsp入門,javaeejsp入門

Servlet寫java代碼很好,但是拼接html的時候,非常不方便

JSP可以在html中嵌套java代碼,這樣在展示的時候,就會比較方便

 

Tomcat幫我們把jsp的頁面翻譯成了Servlet去啟動並執行,查看目錄

Tomcat目錄\work\Catalina\localhost\

產生一個xxx_jsp.java的Servlet檔案,jsp的指令碼片段放在了_jspService()方法裡面

 

使用eclipse建立項目以後,修改一下發布路徑,直接發布到Tomcat的webapps目錄下,

在Servers標籤欄,先刪除下面的項目,在右鍵點open,選擇use Tomcat installation

 

jsp聲明:<%! 若干java代碼%>,此處的代碼會解析到類的成員的地方

jsp注釋:<%--注釋的內容--%>

index.jsp

<%@page import="org.apache.jasper.tagplugins.jstl.core.Out"%><%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>首頁</title></head><body><%!public void test(){    int a=0;}%><% for(int i=0;i<5;i++){    out.write(i+"");}%><%-- Date date=new Date();out.write(date.toLocaleString());--%>你好,JSP</body></html>

index_jsp.java

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {public void test(){    int a=0;} public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)        throws java.io.IOException, javax.servlet.ServletException {    final javax.servlet.jsp.PageContext pageContext;    javax.servlet.http.HttpSession session = null;    final javax.servlet.ServletContext application;    final javax.servlet.ServletConfig config;    javax.servlet.jsp.JspWriter out = null;    final java.lang.Object page = this;    javax.servlet.jsp.JspWriter _jspx_out = null;    javax.servlet.jsp.PageContext _jspx_page_context = null;    try {      response.setContentType("text/html; charset=utf-8");      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("\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");      out.write("<html>\r\n");      out.write("<head>\r\n");      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n");      out.write("<title>首頁</title>\r\n");      out.write("</head>\r\n");      out.write("<body>\r\n");      out.write('\r');      out.write('\n'); for(int i=0;i<5;i++){    out.write(i+"");}      out.write('\r');      out.write('\n');      out.write("\r\n");      out.write("你好,JSP\r\n");      out.write("</body>\r\n");      out.write("</html>");    } catch (java.lang.Throwable t) {      if (!(t instanceof javax.servlet.jsp.SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try {            if (response.isCommitted()) {              out.flush();            } else {              out.clearBuffer();            }          } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);        else throw new ServletException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.