我正在使用JSP创建一个Web应用程序。突然出现"Tomcat v8.5 Server at localhost failed to start"
错误。我尝试了不同的方法,并找到了一种解决方法,方法是在web.xml
文件中添加 element标签。通过添加此选项,我的应用程序将正确执行。
但这导致我没有在JSP中获得 context-param 值。 请帮助我了解其背后的概念。
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<element>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>abc</servlet-name>
<jsp-file>WebContent/contextDemo.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>d</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>
</element>
action.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br>
<input type="submit" value="Search"></form>
</body>
</html>
contextDemo.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% out.print("Welcome"+request.getParameter("uname"));
String driver=application.getInitParameter("d");
out.print("driver name is="+driver);
%>
</body>
</html>