gpt4 book ai didi

java - 如何在我的jsp文件中调用和运行java类方法

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:21 25 4
gpt4 key购买 nike

我尝试使用 jsp 文件中下面提供的代码调用我的 java 方法。我正在一个 Maven 项目中工作。

我面临的错误是无法编译 JSp 的类。我该如何解决它?如何从 JSP 调用我的类方法?

我的index.jsp 文件中的以下代码

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="myStyle.css">
</head>

<body>
<%
Test t = Test new Test();
t.test();
%>

<h2 style="color:red" align="center">Blog</h2>

<div align="center">

<h4> Add new user</h4>

<input class="myTextBox" type="text" placeholder="Enter username"><br/><br/>
<input class="myTextBox" type="password" placeholder="Enter password"><br/><br/>
<Button> Add user </Button>

</div>

</body>

这是我的java代码。

public class Test {

public void test()
{
System.out.println("My name is ");
}

}

错误

HTTP Status 500 - Unable to compile class for JSP:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [14] in the generated java file: [C:\Users\saryal\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\magpie\org\apache\jsp\index_jsp.java]
The import Test cannot be resolved

An error occurred at line: 13 in the jsp file: /index.jsp
Test cannot be resolved to a type
10:
11: <body>
12: <%
13: Test t = Test new Test();
14: t.test();
15: %>
16:


An error occurred at line: 13 in the jsp file: /index.jsp
Syntax error, insert ":: IdentifierOrNew" to complete ReferenceExpression
10:
11: <body>
12: <%
13: Test t = Test new Test();
14: t.test();
15: %>
16:


An error occurred at line: 13 in the jsp file: /index.jsp
Syntax error, insert ";" to complete LocalVariableDeclarationStatement
10:
11: <body>
12: <%
13: Test t = Test new Test();
14: t.test();
15: %>
16:


An error occurred at line: 13 in the jsp file: /index.jsp
Test cannot be resolved to a variable
10:
11: <body>
12: <%
13: Test t = Test new Test();
14: t.test();
15: %>
16:


An error occurred at line: 13 in the jsp file: /index.jsp
Test cannot be resolved to a type
10:
11: <body>
12: <%
13: Test t = Test new Test();
14: t.test();
15: %>
16:


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.26 logs.

Apache Tomcat/8.0.26

最佳答案

An error occurred at line: 13 in the jsp file: /index.jsp

Test cannot be resolved to a type

 Test t = Test new Test();//the problem with this statement

<%
Test t = new Test();//should be, object will be created
t.test();
%>

关于java - 如何在我的jsp文件中调用和运行java类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com