在这段时间学习javaweb时,因为是自己找的视频,下载了最新版本的Tomcat,Tomcat 10 ,然后就发现了很多问题,Tomcat 10 跟之前的版本 有很大的区别,以至于,10以下的版本的文件,到10不能正常运行,通过了解和查询,我明白了其中最重要的Servlet包的路径变了,之前都是javax包下的,现在变成了jakarta包,导致变化很多。
这次的问题时jsp九大内置对象,在jsp文件中先是不能正常引用,只有pageContext对象可以有方法。别的对象都没有方法。别的方法都点不出来方法。
最终我发现这些对象是javax.servlet–api包下的,我就取Maven仓库找到了对应的包,然后导入依赖。
但是但是,新BUG来了。。。。
卧槽,我还以为我的包导错了,但是并不是这个问题。
然后我通过jsp文件生成的java文件看看生成的源码,发现我写的东西连进去都没进去。。
我也不知道为啥,然后搜好像也搜不太出来,
但是我也发现一个问题,它源码中定义的内置对象所属的包是jakarta.servlet 但是我导的包是javax的,但是问题又来了,内置对象不用new是吧,可以直接生成,我生成的,鼠标在上面悬浮,显示的结果是他是javax包下的,我甚至都把那个javax什么依赖的jsp–api删了,但是还是没用。。。
不说了,上目录结构和源码,各位大神,好哥哥好姐姐帮孩子看看怎么回事吧。谢谢大佬们了谢谢谢谢~~~~~
依赖:
<%@ page import="com.sun.xml.internal.messaging.saaj.soap.ver1_1.Header1_1Impl" %><%--
Created by IntelliJ IDEA.
User: 昭昭
Date: 2022/11/17
Time: 15:10
To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>text</h1>
<%--内置对象--%>
<%
pageContext.setAttribute("name1","zz1");
request.setAttribute("name1","zz1");
session.setAttribute("name1","zz1");
application.setAttribute("name1","zz1");
%>
<%--脚本片段中的代码,会被原封不动的生成到jsp.java文件中
要求:这里的代码:必须要保证java语法的正确性
--%>
<%
//从pageContext取出,
//作用域从低到高
String name1 =(String) pageContext.findAttribute("name1");
String name2 =(String) pageContext.findAttribute("name2");
String name3 =(String) pageContext.findAttribute("name3");
String name4 =(String) pageContext.findAttribute("name4");
String name5 =(String) pageContext.findAttribute("name5");
%>
<%--使用EL表达式输出--%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<%=name5%>
<%pageContext.forward("/index.jsp");%>
</body>
</html>
生成的java源码:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/10.0.27
* Generated at: 2022-11-19 08:22:52 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.jsp.*;
public final class PageContextDemo01_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final jakarta.servlet.jsp.JspFactory _jspxFactory =
jakarta.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("jakarta.servlet");
_jspx_imports_packages.add("jakarta.servlet.http");
_jspx_imports_packages.add("jakarta.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile jakarta.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public jakarta.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final jakarta.servlet.http.HttpServletRequest request, final jakarta.servlet.http.HttpServletResponse response)
throws java.io.IOException, jakarta.servlet.ServletException {
if (!jakarta.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
final java.lang.String _jspx_method = request.getMethod();
if ("OPTIONS".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
return;
}
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
return;
}
}
final jakarta.servlet.jsp.PageContext pageContext;
jakarta.servlet.http.HttpSession session = null;
final jakarta.servlet.ServletContext application;
final jakarta.servlet.ServletConfig config;
jakarta.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
jakarta.servlet.jsp.JspWriter _jspx_out = null;
jakarta.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("rn");
out.write("rn");
out.write("<html>rn");
out.write("<head>rn");
out.write(" <title>Title</title>rn");
out.write("</head>rn");
out.write("<body>rn");
out.write("rn");
out.write('r');
out.write('n');
out.write("rn");
out.write("rn");
out.write("</body>rn");
out.write("</html>rn");
} catch (java.lang.Throwable t) {
if (!(t instanceof jakarta.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
pom文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhaozho</groupId>
<artifactId>javaweb-jsp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.62</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>2.3.6</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
求求了,搞了好几天了
在某一天睡醒,我打开IDEA准备学习,突然发现我的web文件突然不能新建jsp文件了,也就是它不在算是一个web项目了,之前还好好的,然后我去看了看Modules的设置,发现我有个地方的路径爆红了,我看看了看文件的路径,修改之后,就好了,我刚好试了试,启动服务器,发现,我的文件好了,,,
这根本原因其实在建项目的时候,我之前有想到,但是没有测试,因为原项目写了很多,自己还是有点懒,以后要克服这一点,我发现我之前因为在学习阶段嘛,就跟着网课老师建了一个空的Maven项目,我们学习javaweb是需要web项目的,所以就牵扯到将一个普通项目变成web项目,我跟着老师做,因为是几年前的视频,所以我就没办法做到一样,然后我就百度了,然后模模糊糊的建项目,就是在File那里点击Project Structure 选择Modules 添加一个web的Modules,然后就可以了,但是我当时不熟悉,就跟着网上一步步做,然后自己就发现了一个又一个的坑。。。。
还有Tomcat 10 我以为最新的能好一点,没想到稳定性什么的问题,然后现实又给我上了一课,不过有错误,我们通过解决问题的过程也是一种不错的学习,能更好的了解我们所学习的内容,还可能有意外收获呢!!!
原文地址:https://blog.csdn.net/weixin_56555958/article/details/127938994
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_50243.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!