当我尝试运行 JSP 文件(右键单击文件名并在 IntelliJ 中运行)时,我得到
我的JSP代码:
`<%@ page contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="sites.css" type="text/css" />
<title>Add</title>
<link rel="stylesheet" href="button.css" type="text/css" />
</head>
<body>
<div align="center">
<h1>Add Company</h1><br>
</form>
<br><br>
<p>fill the form below to add company:</p>
<form action="add" method="post">
<table style="width:80%">
<tr>
<td>Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td>Register Number</td>
<td><input type="text" name="register_number" /></td>
</table><br>
<table>
<tr>
<td>
<input type="submit" value="Submit" class="buttonbtn"/></td></tr></table>
</form>
<br><br>
<table>
<tr><td><a href="/miniCRM/src/main/webapp/WEB-INF/main.html" class=buttonbtn>Main Page</a></td></tr></table>
</div>
</body>
</html>`
Servlet:
`package com.example.minicrm.controller;
import com.example.minicrm.dao.CompanyDao;
import com.example.minicrm.model.Company;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserServlet
*/
@WebServlet(name = "add")
public class AddCompanyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final CompanyDao CompanyDao = new CompanyDao();
/**
* @see HttpServlet#HttpServlet()
*/
public AddCompanyServlet() {
super();
System.out.println("ok");
// 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
response.getWriter().append("Served at: ").append(request.getContextPath());
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/AddCompany.jsp");
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String register_number = request.getParameter("register_number");
Company company = new Company();
company.setName(name);
company.setRegister_number(register_number);
try {
CompanyDao.addCompany(company);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/AddCompany.jsp");
dispatcher.forward(request, response);
}
}
`
POM.xml:
`<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>miniCRM</artifactId>
<version>1.0-SNAPSHOT</version>
<name>miniCRM</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.version>5.9.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.84</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>`
添加依赖项:
`<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.84</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>`
没有帮助。
我正在尝试在本地主机上运行它。无法使用 mvn clean-spring boot run 运行它(没有这样的选项)。
我将感谢所有建议。
WEB-INF 的内容不能由客户端直接请求。那是你的类文件、属性文件以及谁知道还有什么东西存放的地方。将文件移到那里之外。