前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[javaEE] jsp入门

[javaEE] jsp入门

作者头像
唯一Chat
发布2019-09-10 16:23:58
4.8K0
发布2019-09-10 16:23:58
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

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

代码语言:javascript
复制
<%@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

代码语言:javascript
复制
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);
    }
  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-06-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档