我有一个名为“diaryApplication”的java bean类,它有以下代码。我的问题是如何使用jsp或scriptlet调用saveUsers()方法,以便我可以将另一个用户添加到我的xml中。我的jsp包含通常的名称,电子邮件,密码等等。
我还有一个用户javabean,它有一个arraylist代码,允许我每次用户注册或注册时都将用户添加到xml代码中,此代码低于diaryapplication。 我还有一个“用户”Java bean,代码低于那个。 请帮帮我!
package uts.wsd;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class DiaryApplication {
private String filePath;
private Users users;
public DiaryApplication(String filepath, Users users) {
super();
this.filePath = filepath;
this.users = users;
}
public DiaryApplication() {
super();
// TODO Auto-generated constructor stub
}
public String getFilepath() {
return filePath;
}
public void setFilePath(String filePath) throws JAXBException, IOException {
this.filePath = filePath;
// This is the file path given to us.
// We should use it
// Load the users from the XML file...
JAXBContext jc = JAXBContext.newInstance(Users.class);
Unmarshaller u = jc.createUnmarshaller();
FileInputStream fin = new FileInputStream(filePath); // use the given file path
users = (Users)u.unmarshal(fin); // This loads the "users" object
fin.close();
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
// this is the code im trying to call???
public void saveUsers() throws JAXBException, IOException{
// Create the unmarshaller
JAXBContext jc = JAXBContext.newInstance(Users.class);
Unmarshaller u = jc.createUnmarshaller();
// Now unmarshal the object from the file
FileInputStream fin = new FileInputStream(filePath);
users = (Users)u.unmarshal(fin); // This loads the "shop" object
fin.close();
}
}
////用户代码低于
package uts.wsd;
import java.util.*;
import java.io.Serializable;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "users")
public class Users implements Serializable {
// The list of user elements does NOT have an extra wrapper element.
// See the comment in the XML file, and compare to the bookshop example.
@XmlElement(name = "user")
private ArrayList<User> list = new ArrayList<User>();
public ArrayList<User> getList() {
return list;
}
public void addUser(User user) {
list.add(user);
}
public void removeUser(User user) {
list.remove(user);
}
public User login(String email, String password) {
// For each user in the list...
for (User user : list) {
if (user.getEmail().equals(email) && user.getPassword().equals(password))
return user; // Login correct. Return this user.
}
return null; // Login incorrect. Return null.
}
}
////////////用户javabean
package uts.wsd;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class User implements Serializable {
@XmlElement(name = "email")
private String email;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "password")
private String password;
@XmlElement(name = "gender")
private String gender;
@XmlElement(name = " favouritecolour")
private String favouritecolour;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String email, String name, String password, String gender, String favouritecolour) {
super();
this.email = email;
this.name = name;
this.password = password;
this.gender = gender;
this.favouritecolour = favouritecolour;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getFavouritecolour() {
return favouritecolour;
}
public void setFavouritecolour(String favouritecolour) {
this.favouritecolour = favouritecolour;
}
}
package uts.wsd;
import java.util.*;
import java.io.*;
import javax.xml.bind.*;
public class TestJAXB implements Serializable {
public static void main(String[] args) throws Exception {
Users users = new Users();
users.addUser(new User("blaslsa@asda.com", "name", "password", "male", "green"));
users.addUser(new User("average@bloggs.com", "Joe average", "password", "male", "yellow"));
// Boilerplate code to convert objects to XML...
JAXBContext jc = JAXBContext.newInstance(Users.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(users, System.out);
}
}
///注册用户的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<email>enemail@email.com</email>
<name>name of user</name>
<password>blahblah</password>
<gender>male</gender>
<favouriteColour>green</favouriteColour>
</user>
<user>
<email>joe@bloggs.com</email>
<name>Joe Bloggs</name>
<password>foobar</password>
<gender>male</gender>
<favouriteColour>yellow</favouriteColour>
</user>
<user>
<email>average@email.com</email>
<name> my name </name>
<password>my password</password>
<gender>male</gender>
<favouriteColour>yellow</favouriteColour>
</user>
</users>
///这是注册页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="welcome.jsp" method="POST">
<table>
<tr><td>Email</td><td><input type="text" name="email"></td></tr>
<tr><td>Full name</td><td><input type="text" name="name"></td></tr>
<tr><td>Password</td><td><input type="password" name="password"></td></tr>
<tr><td>Gender</td><td><input type="radio" name="gender" value="male">Male<br><input type="radio" name="gender" value="female">Female</td></tr>
<tr><td>Favourite colour</td><td><select name="favcol"><option>red<option>green</select></td></tr>
<tr><td>Agree to TOS</td><td><input type="checkbox" name="tos"></td></tr>
<tr><td></td><td><input type="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>
/// this is the welcome page for users
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="uts.wsd.*"%>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String favouritecolour = request.getParameter("favcol");
String agreeTOS = request.getParameter("tos");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="<%= favouritecolour %>">
<%
if(agreeTOS != null)
{
User user = new User(email,name,password,gender,favouritecolour);
session.setAttribute("user",user);
%>
<p>Welcome, <%= name %>!</p>
<p>Your Email is <%= email %>.</p>
<p>Your password is <%= password %>.</p>
<p>Your gender is <%= gender %>.</p>
<p>Your favourite colour is <%= favouritecolour %>.</p>
<p>Click<a href="index.jsp">Here</a> to go back to mainpage</p>
<%
}
else
{
%>
<p>Sorry, you must agree to the Terms of Services.</p>
<p>Click <a href="register.jsp">here</a> to go back.</p>
<%
}
%>
</body>
</html>
// this is the login page for users
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login</h1>
<table>
<form action="loginaction.jsp" method="POST">
<tr>
<td>Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="Submit" name="Submit"></td>
</tr>
</form>
</table>
</body>
</html>
//////这是我的index.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="uts.wsd.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Diary Keeper</h1>
<% User user = (User)session.getAttribute("user"); %>
<% if(user != null){%>
<div style= "text-align: right; border: solid 1px black;">
You are logged in as <%= user.getName() %> < <%= user.getEmail() %>>
</div>
<div style="text-align: right;">
<a href="logout.jsp">Logout</a>
</div>
<% } else { %>
<div style="text-align: right; border: solid 1px black;">
<p>You are not logged in. <a href="register.jsp"> Register</a> | <a href="login.jsp"> Login</a>
</div>
<% } %>
</body>
</html>
////////这是我的登录页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="uts.wsd.*"%>
<% String filePath = application.getRealPath("WEB-INF/users.xml"); %>
<jsp:useBean id="diaryApp" class="uts.wsd.DiaryApplication" scope="application">
<jsp:setProperty name="diaryApp" property="filePath" value="<%=filePath%>"/>
</jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String email = request.getParameter("email");
String password = request.getParameter("password");
Users users = diaryApp.getUsers();
User user = users.login(email,password);
if(user != null){
session.setAttribute("user", user);
%>
<p>Login successful Click<a href="index.jsp">here</a> to go to main page</p>
<%} else { %>
<p> Password or username incorrect Click<a href="login.jsp"> here</a> to try again.</p>
<% } %>
</body>
</html>