JSP Servlet self-implementation, jspservlet implementation

Source: Internet
Author: User

JSP Servlet self-implementation, jspservlet implementation

Servlet self-implementation in JSP

Step 1

1. inherit from HttpServlet
2. Rewrite the doGet () or doPost () method.
3. Register Servlet in web. xml

2. HttpServlet class inheritance Diagram

Three instances

1. Create a MyFirstServletDemo web project.

2. Edit index. jsp

<% @ Page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

3. Compile HelloServlet

Package servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; // inherit from HttpServletpublic class HelloServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws S ErvletException, IOException {// TODO Auto-generated method stub System. out. println ("processing get () requests... "); PrintWriter out = response. getWriter (); response. setContentType ("text/html; charsets = UTF-8"); out. println ("<strong> Hello Servlet! </Strong> <br> ") ;}@ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System. out. println ("processing post () requests... "); PrintWriter out = response. getWriter (); response. setContentType ("text/html; charsets = UTF-8"); out. println ("<strong> Hello Servlet! </Strong> <br> ");}}

4. Compile web. xml

<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";> <display-name></display-name>  <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet>  <servlet-name>HelloServlet</servlet-name>  <servlet-class>servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping>   <servlet-name>HelloServlet</servlet-name>   <url-pattern>/servlet/HelloServlet</url-pattern> </servlet-mapping></web-app> 

Iv. Running results

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.