欢迎光临
我们一直在努力

Java Servlet 和 JSP入门教程(4)-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

java servlet 和 jsp教程(4)

3.3 输出html的servlet

大多数servlet都输出html,而不象上例一样输出纯文本。要输出html还有两个额外的步骤要做:告诉浏览器接下来发送的是html;修改println语句构造出合法的html页面。

第一步通过设置content-type(内容类型)应答头完成。一般地,应答头可以通过httpservletresponse的setheader方法设置,但由于设置内容类型是一个很频繁的操作,因此servlet api提供了一个专用的方法setcontenttype。注意设置应答头应该在通过printwriter发送内容之前进行。下面是一个实例:

hellowww .java

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(“<!doctype html public \”-//w3c//dtd html 4.0 ” +
“transitional//en\”>\n” +
“<html>\n” +
&nb sp; “<head><title>hello www</title></head>\n” +
“<body>\n” +
“<h1>hello www</h1>\n” +
“</body></html>”);
}
}

3.4 几个html工具函数

通过println语句输出html并不方便,根本的解决方法是使用javaserver pages(jsp)。然而,对于标准的servlet来说,由于web页面中有两个部分(doctype和head)一般不会改变,因此可以用工具函数来封装生成这些内容的代码。

虽然大多数主流浏览器都会忽略doctype行,但严格地说html规范是要求有doctype行的,它有助于html语法检查器根据所声明的 html版本检查html文档合法性。在许多web页面中,head部分只包含<title>。虽然许多有经验的编写者都会在head中包含许多meta标记和样式声明,但这里只考虑最简单的情况。

下面的java方法只接受页面标题为参数,然后输出页面的doctype、head、title部分。清单如下:

servletutilities.java

package hall;

public class servletutilities {
public static final string doctype =
“<!doctype html public \”-//w3c//dtd html 4.0 transitional//en\”>”;

public static string headwithtitle(string title) {
return(doctype + “\n” + “<html>\n” +
“<head><title>” + title + “</title ></head>\n”);
}

// 其他工具函数的代码在本文后面介绍
}

hellowww2.java

下面是应用了servletutilities之后重写hellowww类得到的hellowww2:

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww2 extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(servletutilities.headwithtitle(“hello www”) +
“<body>\n” +
“<h1>hello www</h1> ;\n” +
“</body></html>”);
}
}java servlet 和 jsp教程(4)

3.3 输出html的servlet

大多数servlet都输出html,而不象上例一样输出纯文本。要输出html还有两个额外的步骤要做:告诉浏览器接下来发送的是html;修改println语句构造出合法的html页面。

第一步通过设置content-type(内容类型)应答头完成。一般地,应答头可以通过httpservletresponse的setheader方法设置,但由于设置内容类型是一个很频繁的操作,因此servlet api提供了一个专用的方法setcontenttype。注意设置应答头应该在通过printwriter发送内容之前进行。下面是一个实例:

hellowww .java

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(“<!doctype html public \”-//w3c//dtd html 4.0 ” +
“transitional//en\”>\n” +
“<html>\n” +
&nb sp; “<head><title>hello www</title></head>\n” +
“<body>\n” +
“<h1>hello www</h1>\n” +
“</body></html>”);
}
}

3.4 几个html工具函数

通过println语句输出html并不方便,根本的解决方法是使用javaserver pages(jsp)。然而,对于标准的servlet来说,由于web页面中有两个部分(doctype和head)一般不会改变,因此可以用工具函数来封装生成这些内容的代码。

虽然大多数主流浏览器都会忽略doctype行,但严格地说html规范是要求有doctype行的,它有助于html语法检查器根据所声明的 html版本检查html文档合法性。在许多web页面中,head部分只包含<title>。虽然许多有经验的编写者都会在head中包含许多meta标记和样式声明,但这里只考虑最简单的情况。

下面的java方法只接受页面标题为参数,然后输出页面的doctype、head、title部分。清单如下:

servletutilities.java

package hall;

public class servletutilities {
public static final string doctype =
“<!doctype html public \”-//w3c//dtd html 4.0 transitional//en\”>”;

public static string headwithtitle(string title) {
return(doctype + “\n” + “<html>\n” +
“<head><title>” + title + “</title ></head>\n”);
}

// 其他工具函数的代码在本文后面介绍
}

hellowww2.java

下面是应用了servletutilities之后重写hellowww类得到的hellowww2:

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww2 extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(servletutilities.headwithtitle(“hello www”) +
“<body>\n” +
“<h1>hello www</h1> ;\n” +
“</body></html>”);
}
}java servlet 和 jsp教程(4)

3.3 输出html的servlet

大多数servlet都输出html,而不象上例一样输出纯文本。要输出html还有两个额外的步骤要做:告诉浏览器接下来发送的是html;修改println语句构造出合法的html页面。

第一步通过设置content-type(内容类型)应答头完成。一般地,应答头可以通过httpservletresponse的setheader方法设置,但由于设置内容类型是一个很频繁的操作,因此servlet api提供了一个专用的方法setcontenttype。注意设置应答头应该在通过printwriter发送内容之前进行。下面是一个实例:

hellowww .java

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(“<!doctype html public \”-//w3c//dtd html 4.0 ” +
“transitional//en\”>\n” +
“<html>\n” +
&nb sp; “<head><title>hello www</title></head>\n” +
“<body>\n” +
“<h1>hello www</h1>\n” +
“</body></html>”);
}
}

3.4 几个html工具函数

通过println语句输出html并不方便,根本的解决方法是使用javaserver pages(jsp)。然而,对于标准的servlet来说,由于web页面中有两个部分(doctype和head)一般不会改变,因此可以用工具函数来封装生成这些内容的代码。

虽然大多数主流浏览器都会忽略doctype行,但严格地说html规范是要求有doctype行的,它有助于html语法检查器根据所声明的 html版本检查html文档合法性。在许多web页面中,head部分只包含<title>。虽然许多有经验的编写者都会在head中包含许多meta标记和样式声明,但这里只考虑最简单的情况。

下面的java方法只接受页面标题为参数,然后输出页面的doctype、head、title部分。清单如下:

servletutilities.java

package hall;

public class servletutilities {
public static final string doctype =
“<!doctype html public \”-//w3c//dtd html 4.0 transitional//en\”>”;

public static string headwithtitle(string title) {
return(doctype + “\n” + “<html>\n” +
“<head><title>” + title + “</title ></head>\n”);
}

// 其他工具函数的代码在本文后面介绍
}

hellowww2.java

下面是应用了servletutilities之后重写hellowww类得到的hellowww2:

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww2 extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(servletutilities.headwithtitle(“hello www”) +
“<body>\n” +
“<h1>hello www</h1> ;\n” +
“</body></html>”);
}
}java servlet 和 jsp教程(4)

3.3 输出html的servlet

大多数servlet都输出html,而不象上例一样输出纯文本。要输出html还有两个额外的步骤要做:告诉浏览器接下来发送的是html;修改println语句构造出合法的html页面。

第一步通过设置content-type(内容类型)应答头完成。一般地,应答头可以通过httpservletresponse的setheader方法设置,但由于设置内容类型是一个很频繁的操作,因此servlet api提供了一个专用的方法setcontenttype。注意设置应答头应该在通过printwriter发送内容之前进行。下面是一个实例:

hellowww .java

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(“<!doctype html public \”-//w3c//dtd html 4.0 ” +
“transitional//en\”>\n” +
“<html>\n” +
&nb sp; “<head><title>hello www</title></head>\n” +
“<body>\n” +
“<h1>hello www</h1>\n” +
“</body></html>”);
}
}

3.4 几个html工具函数

通过println语句输出html并不方便,根本的解决方法是使用javaserver pages(jsp)。然而,对于标准的servlet来说,由于web页面中有两个部分(doctype和head)一般不会改变,因此可以用工具函数来封装生成这些内容的代码。

虽然大多数主流浏览器都会忽略doctype行,但严格地说html规范是要求有doctype行的,它有助于html语法检查器根据所声明的 html版本检查html文档合法性。在许多web页面中,head部分只包含<title>。虽然许多有经验的编写者都会在head中包含许多meta标记和样式声明,但这里只考虑最简单的情况。

下面的java方法只接受页面标题为参数,然后输出页面的doctype、head、title部分。清单如下:

servletutilities.java

package hall;

public class servletutilities {
public static final string doctype =
“<!doctype html public \”-//w3c//dtd html 4.0 transitional//en\”>”;

public static string headwithtitle(string title) {
return(doctype + “\n” + “<html>\n” +
“<head><title>” + title + “</title ></head>\n”);
}

// 其他工具函数的代码在本文后面介绍
}

hellowww2.java

下面是应用了servletutilities之后重写hellowww类得到的hellowww2:

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hellowww2 extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype(“text/html”);
printwriter out = response.getwriter();
out.println(servletutilities.headwithtitle(“hello www”) +
“<body>\n” +
“<h1>hello www</h1> ;\n” +
“</body></html>”);
}
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java Servlet 和 JSP入门教程(4)-JSP教程,Jsp/Servlet
分享到: 更多 (0)