J
JayJay
Gast
Hallo,
ich hab da ein servlet und hab da probleme mit den if clausen! Erstmal hier der code. Das problem liegt darin , dass die if clauses nicht so richtig funktionieren. Es gibt 2 variablen die von einer HTML seite geholt werden. Saddress und Uaddress, die zwei textfelder sind und man eine email addresse eingibt. Nun ist es so, dass obwohl ich keinen Text eingebe, trotzdem diese "if(subscribeAddress != null){" aufgerufen wird. Obwohl es null ist läuft dieses trotzdem. Das ist nicht das einzige problem sondern alle if clauses funktionieren nicht so wie ich mir das vorstelle obwohl, die Logik "hoffe ich mal" richtig sein müsste. Es kann sein dass ich da was übersehe im Sinne von Servlets und der getParameter() methode.
ich hab da ein servlet und hab da probleme mit den if clausen! Erstmal hier der code. Das problem liegt darin , dass die if clauses nicht so richtig funktionieren. Es gibt 2 variablen die von einer HTML seite geholt werden. Saddress und Uaddress, die zwei textfelder sind und man eine email addresse eingibt. Nun ist es so, dass obwohl ich keinen Text eingebe, trotzdem diese "if(subscribeAddress != null){" aufgerufen wird. Obwohl es null ist läuft dieses trotzdem. Das ist nicht das einzige problem sondern alle if clauses funktionieren nicht so wie ich mir das vorstelle obwohl, die Logik "hoffe ich mal" richtig sein müsste. Es kann sein dass ich da was übersehe im Sinne von Servlets und der getParameter() methode.
Code:
package servlet;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Subscription3 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//first, set the "content type" header of the response
res.setContentType("text/html");
//Get the response's PrintWriter to return text to the client.
PrintWriter toClient = res.getWriter();
String subscribeAddress= req.getParameter("Saddress");
String unsubscribeAddress= req.getParameter("Uaddress");
if(subscribeAddress != null){
try {
// Adding the E-Mail address from the list
File appendAddress = new File("C:/Programme/Tomcat/apache-tomcat-6.0.13/webapps/JME/WEB-INF/classes/mail/address.txt");
String filename = appendAddress.toString();
boolean append = true;
FileWriter fw = new FileWriter(filename, append);
fw.write(subscribeAddress + "\r\n");
fw.close(); // closing writing
// Respond to client with a successful request message
toClient.println("<html>");
toClient.println("<title>Request successful</title>");
toClient.println("<body bgcolor='yellow'>");
toClient.println("
");
toClient.println("<p ALIGN='CENTER'><Face Size='16' FACE='ARIAL, HELVETICA'>");
toClient.println("Congratulations! You have been subscribed to the Java Monitoring Service");
toClient.println("</p>");
toClient.println("</body>");
toClient.println("</html>");
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(subscribeAddress == null) {
// Deleting the E-Mail address from the list
try {
// Compare to every line if == then delete address
FileInputStream stream = new FileInputStream("C:/Programme/Tomcat/apache-tomcat-6.0.13/webapps/JME/WEB-INF/classes/mail/address.txt"); // Streaming in the email file
DataInput reader = new DataInputStream(stream);
String ausgabe = "";
while ((ausgabe = reader.readLine()) != null) { // reading in addresses line by line
if (unsubscribeAddress==subscribeAddress) {
subscribeAddress.replace(ausgabe, "");
}
}
// Respond to client with a successful removed message
toClient.println("<html>");
toClient.println("<title>Request successful</title>");
toClient.println("<p ALIGN='CENTER'><Face Size='16' FACE='ARIAL, HELVETICA'>");
toClient.println("Your E-Mail address has been removed from the Java Monitoring Service");
toClient.println("</p>");
toClient.println("</html>");
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (subscribeAddress == null && unsubscribeAddress == null) {
toClient.println("No Text was entered. Please try again");
}
}
}