Hi,
I am trying to implement MVC to write sort of a quiz application. Currently, I have a couple of text files with questions and players with their scores. At the moment I been able to read the players textfile and put it into a hashmap object which will be displayed on the login page.
At the login page the user will enter a username, if already in the hashmap then user is not allowed to play otherwise the first set of questions are loaded.
this is the code that i currently have for my servlet:
=======================
import java.io.IOException;
//import java.util.HashMap;
import javax.servlet.ServletExcep
tion;
import javax.servlet.http.HttpSer
vlet;
import javax.servlet.http.HttpSer
vletReques
t;
import javax.servlet.http.HttpSer
vletRespon
se;
//import javax.servlet.http.HttpSes
sion;
public class LoadPage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = Util.readPlayers(this, "/players.txt").toString()
;
getServletContext().setAtt
ribute("me
ssage" , data);
request.getRequestDispatch
er("/Login
.jsp").for
ward(reque
st, response);
}
}
this is the readplayers method:
==================
public class Util {
static HashMap readPlayers(HttpServlet srv, String fname) {
InputStream is = srv.getServletContext().ge
tResourceA
sStream(fn
ame);
Scanner in = new Scanner(is);
HashMap hm = new HashMap();
String line;
while (in.hasNext()) {
line = in.nextLine();
StringTokenizer st = new StringTokenizer(line);
String name = st.nextToken();
String score = st.nextToken();
hm.put(name,score);
}
return hm;
}
==========================
==
1. I am having problems displaying the contents of the hashmap on the login page
2. Also how do i read from the questions file? do i use a bean? and how do i write to the playersfile.
this is just the beginning, i know i will have more questions which i will post later. Please help
Thanks
Start Free Trial