(Servlet learning Note) A small example of a servlet

Source: Internet
Author: User

Xml

<?xml version= "1.0" encoding= "Utf-8"?> <!  DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD >     <web-app>  <display-name></display-name>  <servlet>    < Servlet-name>myservlet</servlet-name>    <servlet-class>cn.my.servlet.myservlet</ servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>myservlet </servlet-name>    <url-pattern>/MyServlet</url-pattern>  </servlet-mapping>      <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </ Welcome-file-list></web-app>
View Code

Java code

 PackageCn.my.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.sun.prism.Mesh;/*** Servlet Implementation class Myservlet*/ Public classMyservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; PrivateString message;  Public voidInit ()throwsservletexception {Message= "One Sevlet"; }    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Setting the response content typeResponse.setcontenttype ("text/html"); //Set EncodingResponse.setcharacterencoding ("Utf-8"); //CodePrintWriter writer =Response.getwriter (); Writer.println ("); }    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
View Code

JSP code

<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body>    </Body></HTML>
View Code

Results:

(Servlet learning Note) A small example of a servlet

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.