【lab4】Maven創建Web項目案例

遇到問題:
1.字符集亂碼—》utf-8和GBK直接換下試試;
2.HTTP Status 404 – Not Found—》web.xml沒配好,或者代碼寫錯了。。

在這裏插入圖片描述
1.配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sise</groupId>
  <artifactId>MavenWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
        <!-- https://mvnrepository.com/artifact/javax/javaee-web-api -->
	<dependency>
	    <groupId>javax</groupId>
	    <artifactId>javaee-web-api</artifactId>
	    <version>8.0.1</version>
	    <scope>provided</scope>
	</dependency>
	
  </dependencies>
  <build>
    <finalName>MavenWeb</finalName>
  </build>
</project>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h2>Hello World!</h2>
<form action="/MavenWeb/WelcomeServlet" method="post">
   請輸入問候名字:<input type='text' name="name" /><br/>
   <input type='submit' name="問候">
</form>
</body>
</html>
<%@ page  contentType="text/html" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
   問候信息:<%=request.getAttribute("welcome")%>
</body>
</html>
package com.sise.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	
	public WelcomeServlet() {
		// TODO Auto-generated constructor stub
		super();
		
	}
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String welcome = "Hello,"+name;
		request.setAttribute("welcome", welcome);
		request.getRequestDispatcher("/welcome.jsp").forward(request, response);
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
	
		doGet(request, response);
	}
}
<!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>Archetype Created Web Application</display-name>
   <servlet>
  	<servlet-name>WelcomeServlet</servlet-name>
  	<display-name>WelcomeServlet</display-name> 
  	<description></description>
  	<servlet-class>com.sise.servlet.WelcomeServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>WelcomeServlet</servlet-name>
  	<url-pattern>/WelcomeServlet</url-pattern>
  </servlet-mapping>
	
</web-app>
發佈了28 篇原創文章 · 獲贊 7 · 訪問量 4674
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章