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

时间:2015-09-22 19:07:30

标签: java html jsp

我尝试使用下面在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

2 个答案:

答案 0 :(得分:0)

  

jsp文件中的第13行:/index.jsp

发生错误      

无法将测试解析为类型

/document:
  post:
    summary: Api Summary
    description: Api Description
    consumes:
      - multipart/form-data
    parameters:
      - name: documentDetails
        in: formData
        description: Document Details
        required: true
        schema:
          $ref: '#/definitions/Document'
      - name: document
        in: formData
        description: The actual document
        required: true
        type: file

答案 1 :(得分:0)

除了Satya指出的问题之外,您可能必须使用import语句向JSP添加页面指令。这样的事情:

<%@ page import="your.package.Test" %>