Javaweb基础学习3

目录

    • JavaWeb基础
    • Servlet
        • HelloServlet

JavaWeb基础

Servlet

Servlet简介

  • Servlet就是sun公司开发动态web的一门技术
  • 开发Servlet程序两个步骤:
    • 编写一个类
    • 把开发好的Java类部署到web服务器中

HelloServlet

  • 创建一个maven项目,删掉里面的scr文件夹,之后在这里面建立Moudel。

  • 创建maven子工程:使用eclipse创建maven的父子工程_NancyWu_LuckyGirl的博客-CSDN博客_eclipse创建父子工程

  • 关于The parent project must have a packaging type of POM问题:新建maven module时报错 The parent project must have a packaging type of POM_frankliu01的博客-CSDN博客

maven环境优化

  • 修改web.xml为最新的:maven里面web.xml的配置文件_weather_d的博客-CSDN博客_mavenwebxml文件

之后在Java包中创建HelloServlet类,并继承HttpServlet

package com.hai;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * Servlet implementation class Helloservlet
 */
public class Helloservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Helloservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("heelo");
		response.getWriter().append("Served at:heelo ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • 编写servlet映射

在web.xml下编写


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" metadata-complete="true" version="5.0">
  <description>
      Servlet and JSP Examples.
    description>
  <display-name>Servlet and JSP Examplesdisplay-name>

<servlet>
    <description>description>
    <display-name>Helloservletdisplay-name>
    <servlet-name>Helloservletservlet-name>
    <servlet-class>com.hai.Helloservletservlet-class>
  servlet>

      <servlet-mapping>
    <servlet-name>Helloservletservlet-name>
    <url-pattern>/hellourl-pattern>
  servlet-mapping>
  
 <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.xhtmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  
web-app>
  • 添加tomcat服务器
  • 测试

注:
谨慎参考,还有很多问题没解决
补:

  1. 第一个父类maven项目要用pom打包类型,第二个用war类型(创建时要勾选上create a simple project)
  2. 我用的是ecplise,在第一个父类maven中导入了jakarta.servlet的包,可以在网上搜索maven仓库找(不知道什么原因,在Java包中的HelloServlet类用javax测试时会出现404,所有我改用Jakarta

<dependency>
    <groupId>jakarta.servletgroupId>
    <artifactId>jakarta.servlet-apiartifactId>
    <version>5.0.0version>
    <scope>providedscope>
dependency>

你可能感兴趣的:(1024程序员节,servlet)