Sunday, 18 August 2013

reading from a text file in servlets

reading from a text file in servlets

i have a login servlet from where i take a username and a password. i have
a credentials.txt file where i have saved a few usernames followed by
their passwords adjacently in a single line. once i read the username and
password in my logincheck servlet, i want to search it in credentials.txt.
if a match is found, we are directed to a welcomepage servlet, and if not
found, we are again directed to the login servlet. i am getting array out
of bounds exception in my code.
Plz help correct my code.
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String user=request.getParameter("username");
String pass=request.getParameter("password");
File obj=new File("credentials.txt");
FileReader reader=new FileReader(obj);
BufferedReader in=new BufferedReader(reader);
String aks[],temp1,temp2;
int i=0;
String line=in.readLine();
while(line!=null){
aks=line.split("\t");
while(aks[i+1]!=null){
temp1=aks[i];
temp2=aks[i+1];
if(temp1.equals(user) && temp2.equals(pass)){
RequestDispatcher
obj1=request.getRequestDispatcher("welcomepage");
obj1.forward(request,response);
}
line=in.readLine();
}
}
String errormsg="username and password do not match. Please re-enter";
request.setAttribute("errormsg",errormsg);
RequestDispatcher obj1=request.getRequestDispatcher("login");
obj1.forward(request,response);
}

No comments:

Post a Comment