我有一个jsp页面和一个类。我试图在jsp页面中使用该类的信息。这是代码:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="user.Customer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>Main Menu</h1>
<% session.setAttribute("username", Globals.customer.getUsername());
session.setAttribute("password", Globals.customer.getPassword());
session.setAttribute("name", Globals.customer.getName());
session.setAttribute("surname", Globals.customer.getSurname());
session.setAttribute("phone", Globals.customer.getPhone());
session.setAttribute("address", Globals.customer.getAddress());
session.setAttribute("email", Globals.customer.getEmail());%>
<a href="editinfo.jsp">Edit your personal information</a>
</body>
</html>
全球类:
import user.Customer;
public class Globals {
public static Customer customer;
}
客户类:
package user;
public class Customer {
public Customer(){}
public Customer(String username,String password,String name,String surname,String phone,String address,String email){
this.username=username;
this.password=password;
this.name=name;
this.surname=surname;
this.phone=phone;
this.address=address;
this.email=email;
}
private String username;
private String password;
private String name;
private String surname;
private String phone;
private String address;
private String email;
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
public String getEmail(){
return email;
}
public String getPhone(){
return phone;
}
public String getAddress(){
return address;
}
}
以下是我的工作:当用户登录时,我首先通过设置用户名,电子邮件等创建一个Customer对象,然后将其添加到当前会话中。但是在行
session.setAttribute("username", Globals.customer.getUsername()
它给出了一个错误说
An error occurred at line: 14 in the jsp file: /main.jsp
Globals cannot be resolved
11: <body>
12:
13: <h1>Main Menu</h1>
14: <% session.setAttribute("username", Globals.customer.getUsername());
15: session.setAttribute("password", Globals.customer.getPassword());
16: session.setAttribute("name", Globals.customer.getName());
17: session.setAttribute("surname", Globals.customer.getSurname());
任何人都可以帮我吗?感谢
答案 0 :(得分:1)
使用页面指令或通过Globals类扩展该jsp页面
即<%@page import="package.Globals" %>
包是你的包的名称..
答案 1 :(得分:1)
导入您的Globals
课程
<%@ page import="user.Customer" %>
<%@ page import="path.to.Globals" %>
或使用单一页面指令进行导入,例如
<%@ page import="user.Customer,path.to.Globals" %>