Eclipse在jsp中使用Classes [英] Eclipse using Classes in jsp

查看:181
本文介绍了Eclipse在jsp中使用Classes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jsp文件中使用自己的类,我不能只是解决问题,我知道有一些线程,但仍然无法让它工作。

Hi i trying to using my own classes in a jsp file and i cant just resolve the problem, i know there is some threads about it but still i cant get it to work.

我有这个类 Hej.java

public class Hej {
 String a;

 public Hej(String a){
        this.a = a;
 }
 public String hej() {
        return a;
 }
}

这里是我的jsp文件 Newfile。 jsp

and here are my jsp file Newfile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="Hej" %>

<html>
   <head>
   </head>
   <body>
      <%Hej a = new Hej(); %>
      <%=a.hej() %>
   </body>
</html>

我的文件夹看起来像这样

my folders look like this

Projectname
           Java Resources
                         src
                            (default package)
                                             Hej.java
           WebContent
                     NewFile.jsp


推荐答案

首先不要使用scriptlet来执行任何逻辑
,其次你的代码

First of all dont use scriptlet for any logic to be implemented and secondly your code

<%Hej a = new Hej(); %>

失败,因为您在类中有一个参数化的构造函数,您正在初始化一个没有参数的对象尝试ths

fails because you have a parameterized constructor in your class by you are initializing an object without an argument try ths

<% Hej a = new Hej("Hello World !"); %>

还有一件事,而不是使用默认包创建一些包。

One more thing instead of using default package create some package.

示例创建一个命名为 mypackage 并拖动它里面的类。然后将页面导入更改为如下所示:

Example create a package named mypackage and drag the class inside it. then change the page import to something like this :

<%@ page import="mypackage.Hej" %>

这篇关于Eclipse在jsp中使用Classes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆