栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第六章 JSP的上机练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第六章 JSP的上机练习

jsp技术
  • 编写第一个jsp文件
  • JSP基本语法

编写第一个jsp文件

在WebRoot下新建一个HelloWord.jsp

  
    Welcome to yj's JSP 


然后找到workplace文件夹有

在C:UsersAdministratorWorkspacesMyEclipse Professional 2014.metadata.me_tcatworkCatalinalocalhostChapter06orgapachejsp文件夹下可以找到
可以看出JSP文件被转换成了.java,.class文件

打开java文件可以看到源代码

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;

public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspbase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.expressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getexpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    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 {
      response.setContentType("text/html;charset=ISO-8859-1");
      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');
      out.write('n');

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

      out.write("rn");
      out.write("rn");
      out.write("rn");
      out.write("rn");
      out.write("  rn");
      out.write("    rn");
      out.write("    rn");
      out.write("    My JSP 'HelloWorld.jsp' starting pagern");
      out.write("    rn");
      out.write("trn");
      out.write("trn");
      out.write("t    rn");
      out.write("trn");
      out.write("trn");
      out.write("trn");
      out.write("rn");
      out.write("  rn");
      out.write("  rn");
      out.write("  rn");
      out.write("    Welcome to yj's JSP 
rn"); out.write(" rn"); out.write("rn"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
JSP基本语法

在head里面使用<%! %>声明变量,定义了两个变量a,b以及print()方法,在body里面运行使用了<% %>输出了两个常量的乘积,以及print()方法中的返回信息。
代码为:

	 
	<%!int a=3,b=2; %>
	<%!public String print(){
	   String str="you are the lucky girl!";
	   return str;}
	 %>

  
  
  
    <% out.println(a*b);%>
    
<%out.println(print()); %>

再来一个例子:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    我的网页


<%
    int i=0;
    out.print(i+"号");
%>

<%=i+1 %>
<%! public class Student { int age; String name; public Student(String name,int age) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + ''' + '}'; } } %> <% Student girl = new Student("踌躇樱桃", 8); out.println(girl.toString()); %> <%--注释一下 --%>

在注释的时候,页面中不显示

可以再查看源代码中查看

可以看出jsp的注释信息没有显示,因为在tomcat编译jsp文件时,注释会被忽略掉。但是对于html的注释会认为是内容传送到网页上。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号