Eclipse develops a simple counter for servlet+jsp mode __js

Source: Internet
Author: User
Tags stub
Eclipse Development servlet+jsp Model of the simple counter 1, click File->javaproject->tomcatproject, input project name; 2, under the project to create a new index.jsp page as Home 3, in the Web-inf directory to create a new Web.xm file, write the following content. XML version = "1.0" encoding = "UTF-8"?>
< Web-app Version = "2.5" xmlns = "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "HTTP://JAVA.SUN.COM/XML/NS/JAVAEE; Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

< servlet >
< Servlet-name >countservlet </servlet-name >
< Servlet-class >cn.sdut.chenqiang.countservlet </servlet-class >
</servlet >
< servlet-mapping >
< Servlet-name >countservlet </servlet-name >
< Url-pattern >/countservlet </url-pattern >
</servlet-mapping >
< Welcome-file-list >
< Welcome-file >index.jsp </welcome-file >
</welcome-file-list >
</Web-app > 4, New Class Countservlet in Src directory, inheriting httpservlet package Cn.sdut.chenqiang;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import Javax.servlet.ServletConfig;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Countservlet extends HttpServlet {
public static final String content_type= "Text/html;charset=utf-8";

@Override
public void init (ServletConfig config) throws servletexception {
TODO auto-generated Method Stub
Super.init (config);
}

@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
DoPost (req, resp);
}

@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
ServletContext context = Getservletcontext ();
Integer count = (integer) context.getattribute ("Count");

if (count== null)
{
Count = new Integer (0);
Context.setattribute ("Count", new Integer (0));
}
Resp.setcontenttype (Content_Type);
PrintWriter out = Resp.getwriter ();
Out.println ("<HTML>");
Out.println ("<HEAD><TITLE> click Count </TITLE></HEAD>");
Out.println ("<BODY>");
Out.println ("<p> Current count is:" +count+ "</p>");
Out.println ("</BODY></HEAD>");
Count = new Integer (Count.intvalue () +1);
Context.setattribute ("Count", count);
}

@Override
public void Destroy () {
TODO auto-generated Method Stub
Super.destroy ();
}

}

5, run can
Related Article

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.