Servlet Base Routines-helloservlet-nt version

Source: Internet
Author: User
Tags final flush header
servlet/*
Author: He Zhiqiang [hhzqq@21cn.com]
Date: 2000-08-10
Version: 1.0
Features: servlet Base Routines-HelloServlet
*/

Import java.io.*;
Import java.text.*; Messageformat
Import javax.servlet.*;
Import javax.servlet.http.*;

public class HelloServlet extends httpservlet{
Page title
Protected static final String strtitle = "servlet base routines-HelloServlet";

Header
Protected static final String Strheader =
"""<meta http-equiv=\" content-type\ "content=\" text/html; Charset=gb2312\ ">" +
"<title>{0}</title>" +
""<body>";

Footer
Protected static final String Strfooter =
"</body>" +
"
Form
Protected static final String Strform =
"<form action=\" {0}\ "method=\" post\ ">" +
"Your name: <input type=\" text\ "name=\" name\ ">" +
"<input type=\" submit\ "name=\" submit\ "value=\" submit \ ">" +
"</form>";

Protected static final String Strhello =
"Hello, {0}, Welcome to SERVLET/JSP World!" ";

Error message
Protected static final String strerror =
"
protected void doget (HttpServletRequest req,httpservletresponse resp) throws servletexception,ioexception{
Process (REQ,RESP);
}

protected void DoPost (HttpServletRequest req,httpservletresponse resp) throws servletexception,ioexception{
Process (REQ,RESP);
}

protected void process (HttpServletRequest Req,httpservletresponse resp) throws servletexception,ioexception{
try{
String submit = Req.getparameter ("submit");
if (submit==null)
PrintForm (REQ,RESP);
Else
Printhello (REQ,RESP);
}
catch (Exception e) {
Printerror (E.tostring (), REQ,RESP);
}
}

protected void PrintForm (HttpServletRequest req,httpservletresponse resp) throws servletexception,ioexception{
You must set the content Type before using PrintWriter
Resp.setcontenttype ("text/html;charset=gb2312");

PrintWriter out = Resp.getwriter ();

Output header
Out.print (Messageformat.format (strheader,new object[]{strtitle+ "-Please enter your name"});

Output form
Out.print (Messageformat.format (strform,new object[]{req.getcontextpath () +req.getservletpath ()));

Output footer
Out.print (Strfooter);

Out.flush ();
}

protected void Printhello (HttpServletRequest req,httpservletresponse resp) throws servletexception,ioexception{
Get data entered by the user
String name = Req.getparameter ("name");

if (name==null)
Name = "Anonymous";
Else
Make the necessary character encoding conversion for the data entered by the user
name = new String (name.getbytes ("iso-8859-1"));

You must set the content Type before using PrintWriter
Resp.setcontenttype ("text/html;charset=gb2312");

PrintWriter out = Resp.getwriter ();

Output header
Out.print (Messageformat.format (strheader,new object[]{strtitle+ "-Welcome You"});

Output Welcome Information
Out.print (Messageformat.format (Strhello,new object[]{name}));

Output footer
Out.print (Strfooter);

Out.flush ();
}

protected void Printerror (String error, HttpServletRequest Req,httpservletresponse resp) throws Servletexception, ioexception{
You must set the content Type before using PrintWriter
Resp.setcontenttype ("text/html;charset=gb2312");

PrintWriter out = Resp.getwriter ();

Output header
Out.print (Messageformat.format (strheader,new object[]{strtitle+ "-Error Message"});

Output error message
Out.print (Messageformat.format (Strerror,new object[]{error}));

Output footer
Out.print (Strfooter);

Out.flush ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.