在web.xml檔案中加入下面的代碼的話所有頁面都無法直接從IDEA右上角訪問,去掉后就可以訪問。
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.example.myWebsite.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
后面多次嘗試,發現其實是mapping的問題,去點mapping后能正常訪問,請問是什么原因,會不會是servlet撰寫有問題
package com.example.myWebsite;
import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init(ServletConfig config)throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
String name = request.getParameter("name");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>" +
"<head><title>Servlet運行</title></head>" +
"<body><h2>您好"+name+"</h2></body>" +
"</html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
doGet(request, response);
}
public void destroy() {
}
}
jsp代碼:
<%--
Created by IntelliJ IDEA.
User: luo'xin
Date: 2021/5/9
Time: 23:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>歡迎</title>
</head>
<body>
<h2>Servlet</h2>
<form method="post" action="/hello">
姓名:<input name="name" type="text">
<button type="submit">提交</button>
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283606.html
標籤:Web 開發
上一篇:反射呼叫方法,傳引數的問題
