Search code examples
javascriptjspdom-eventsjsp-tags

JSP not working correctly with PrintWriter


I currently have two files, register.html and register.jsp. The HTML file asks the user for some information, and the JSP file is supposed to write information to a text file. I keep getting errors with the file, specifically in the PrintWriter portion. Any help would be much appreciated.

Here is the error I'm getting:

An error occurred at line: 14 in the jsp file: /register.jsp
Duplicate local variable pw
11: 
12: try {   
13: 
14:     PrintWriter pw=null;
15:    pw = new PrintWriter(new FileWriter(new File(nameOfTextFile), true));  
16:    // PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
17:     

register.html:

<%@ page import="java.io.*, java.util.Date"  %>
<DOCTYPE! html>
<HTML>
<HEAD>
<TITLE>Registration</TITLE>
</HEAD>
<BODY>
<h1>Enter the following information to sign and create an account in the Book.</h1>
<form name=registrationForm action="register.jsp">
    <table border=0>
        <tr><td align=right>Username:</td>
        <td><input type=text name="Username" size=25></td>
    </tr>
    <tr><td align=right>Password:</td>
        <td><input type=text name="password" size=25></td>
    </tr>
    <tr><td align=right>Email address:</td>
        <td><input type=text name="email" size=25></td>
    </tr>
    <tr>
    <!--<td valign=middle align=right>Comments:</td>
    <td><TextArea rows=5 cols=35 name="comments">none</TextArea>
    </td>-->
    </tr>
</table>
<br>
<input type="submit" value="Register" name="button" onClick="validatingForm()">
<input type=reset value="Start Over">
</form>
</BODY>
</HTML>

register.jsp:

<%@ page import="java.io.* , java.util.Date"  %>
<HEAD>Thanks for registering!</TITLE></HEAD>
<BODY>
<%
String un =request.getParameter("Username");
String pw =request.getParameter("password");
String nameOfTextFile = "/xampp/tomcat/webapps/gb/userss.txt"; //directory path and name of file
try {   
PrintWriter pw=null;
   pw = new PrintWriter(new FileWriter(new File(nameOfTextFile), true));  
   // PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
    pw.println(un);
    pw.println(pw);
    pw.close();
    } catch(IOException e) {
   out.println(e.getMessage());
}
%>
</BODY>
 </HTML>

Solution

  • As others have said you should move this code into a java class. With that being said I believe your issue is that you have defined two variables named pw.

    String un =request.getParameter("Username");
    String pw =request.getParameter("password"); //FIRST DECLARATION OF PW
    String nameOfTextFile = "/xampp/tomcat/webapps/gb/userss.txt"; //directory path and name of file
    try {   
    PrintWriter pw=null; //SECOND DECLARATION OF PW causes error
       pw = new PrintWriter(new FileWriter(new File(nameOfTextFile), true));  
       // PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
        pw.println(un);
        pw.println(pw);
        pw.close();
        } catch(IOException e) {
       out.println(e.getMessage());
    }
    

    Try:

    String un =request.getParameter("Username");
    String pw =request.getParameter("password");
    String nameOfTextFile = "/xampp/tomcat/webapps/gb/userss.txt"; //directory path and name of file
    try {   
    PrintWriter printWriter=null;
       printWriter = new PrintWriter(new FileWriter(new File(nameOfTextFile), true));  
       // PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
        printWriter.println(un);
        printWriter.println(pw);
        printWriter.close();
        } catch(IOException e) {
       out.println(e.getMessage());
    }