2012-12-12 5 views
0

나는 학교용 온라인 텍스트 편집기를 쓰고 있습니다. 사용자 정의 이름으로 파일을 저장할 수 있었으므로 문제는 아닙니다. 내가 가지고있는 HTML 폼을 가지고 TextArea의 텍스트를 사용자가 지정한 파일에 제출하고 url을 blah.org/text.jsp?status=gen으로 설정합니다. 나중에 코드에서 변수 상태 == gen이면 프로그램에 파일을 쓰게하고 그렇지 않으면 아무것도하지 않습니다. 제출을 클릭하면 파일을 만들 수 없으며 URL에 변수를 가져 오는 것과 관련이 있다고 생각합니다.JSP로 URL 변수를 가져 오지 못했습니다.

/* Set the "user" variable to the "user" attribute assigned to the session */ 
String user = (String)session.getAttribute("user"); 
String status = request.getParameter("status"); 
    /* Get the name of the file */ 
String name = request.getParameter("name"); 
    /* Set the path of the file */ 
String path = "C:/userFiles/" + user + "/" + name + ".txt"; 
/* Get the text in a TextArea */ 
String value = request.getParameter("textArea"); 
    /* If there isn't a session, tell the user to Register/Log In */ 
if (null == session.getAttribute("user")) { 
out.println("Please Register/Log-In to continue!"); 
    if (status == "gen") { 
     try { 
      FileOutputStream fos = new FileOutputStream(path); 
      PrintWriter pw = new PrintWriter(fos); 
      pw.println(value); 
      pw.close(); 
      fos.close(); 
    } 
    catch (Exception e) { 
     out.println("<p>Exception Caught!"); 
    } 
} else { 
out.println("Error"); 
} 
} 

양식 : 여기 내 코드입니다

<form name="form1" method="POST" action="text.jsp?status=gen"> 
<textarea cols="50" rows="30" name="textArea"></textarea> 
<center>Name: <input type="text" name="name" value="Don't put a .ext"> <input type="submit" value="Save" class="button"></center> 
other code here </form> 

답변

0

문자열을 .equals()과 비교하는 대신 ==과 비교하고 있습니다. ==은 문자열이 동일한 문자를 포함하고있는 경우가 아니라 동일한 객체 인스턴스를 비교합니다.

IO tutorial을 읽으십시오. 스트림은 항상 finally 블록에서 닫혀 야하며 Java 7 try-with-resources 구조를 사용해야합니다.

0

if (status.equals("gen")) { 

또는 더 나은와

if (status == "gen") { 

를 교체

if ("gen".equals(status)) { 

첫 번째 문은 문자열 평등이 아닌 개체 평등을 수행합니다. 따라서 항상 false를 반환합니다.

관련 문제