Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Servlet Validation and Redirection Problem

843841Jun 29 2006 — edited Jun 30 2006
Hello,

I have a Validation Servlet that mapped in web.xml so:
    <servlet>
    <servlet-name>Validation</servlet-name>
    <servlet-class>project.Validation</servlet-class>
    </servlet>
    
    <servlet-mapping>
    <servlet-name>Validation</servlet-name>
    <url-pattern>/valid</url-pattern>
    </servlet-mapping>
I use ajax and by doGet(), if all fields was filled correctly send such an XML fragment to the browser:
<valid>
<name>true</name>
<data>true</data>
</valid>
JavaScript get this XML and check, and if all fields was filled correctly, submit the form and redirect user by doPost (Servlet)
context.getRequestDispatcher("/uploadedOK.jsp").forward(request, response);
The user see the following URL in the browser, after submition:
http://localhost:8080/project/valid
If the user copy and paste this link later in the browser, the doGet() method will be executed in the Servlet, the user see the xml fragment:
<valid>
<name>false</name>
<data>false</data>
</valid>
How can I redirect the user, that he instead of
http://localhost:8080/project/valid
see in the browser:
http://localhost:8080/project/uploadedOK.jsp
And how can I redirect the user to the
/project/index.jsp
if the user copied the
http://localhost:8080/project/valid
in his browser. The doGet() method will be always executed with this XML tags and can confuse user. I also don't want the user to call this link, only if it get through the form.

Here is the html form code:
<form id="submitForm" action="valid"
 enctype="MULTIPART/FORM-DATA"
method="post" 
onsubmit="validateUserData(); return false;">

<table align="center" width="330">
    <tr>
		<td colspan="2">
        <b><div id="Message"></div></b>
        </td>
	</tr>
	<tr>
		<td><b><div id="nameMessage">Name:</div></b></td>
		<td><input type="text" name="name" id="nameId" /></td>
	</tr>
	<tr>
		<td><b><div id="dataMessage">Data:</div></b></td>
		<td><input type="text" name="data" id="dataId" /></td>
	</tr>
	<tr>
		<td colspan="2" align="right"><br>
		<input type="submit" value="Sent" /></td>
	</tr>
</table>

</form>
JavaScript code
<script type="text/javascript">

function AJAXInteraction(url, callback) {

    var req = init();
    req.onreadystatechange = processRequest;
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () {
      // readyState of 4 signifies request is complete
      if (req.readyState == 4) {
        // status of 200 signifies sucessful HTTP call
        if (req.status == 200) {
          if (callback) callback(req.responseXML);
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
}

function validateUserData() {
    var name = document.getElementById("nameId");
    var data = document.getElementById("dataId");
    var url = "validate?name=" + encodeURIComponent(name.value) +
    "&data=" + encodeURIComponent(data.value);     
    var name = document.getElementById("nameId");
    var data = document.getElementById("dataId");
    var ajax = new AJAXInteraction(url, validateCallback);
    ajax.doGet();
}

function validateCallback(responseXML) {
  
   var msg = responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
   var isRight = true;

   if (msg == "false"){
       var mdiv = document.getElementById("nameMessage");
       // set the style on the div to invalid
       mdiv.className = "f_invalid";
       mdiv.innerHTML = "Name:";
       isRight = false;
    } else {
       var mdiv = document.getElementById("nameMessage");
       mdiv.className = "f_correct";
       mdiv.innerHTML = "Name:";

    }
    var msg = responseXML.getElementsByTagName("data")[0].firstChild.nodeValue;
    
       if (msg == "false"){
       var mdiv = document.getElementById("dataMessage");
       // set the style on the div to invalid
       mdiv.className = "f_invalid";
       mdiv.innerHTML = "Data:";
       isRight = false;
    } else {
       var mdiv = document.getElementById("dataMessage");
       mdiv.className = "f_correct";
       mdiv.innerHTML = "Data:";

    }

    if (isRight) {
    var mdiv = document.getElementById("Message");
    mdiv.className = "f_correct";
    mdiv.innerHTML = "";
    var mform = document.getElementById("submitForm");
    mform.submit();
    } else {
    var mdiv = document.getElementById("Message");
    mdiv.className = "f_invalid";
    mdiv.innerHTML = "Error.";
    }
    
}

</script>
ServletCode:
package project;

public class Validation extends HttpServlet {
    
    private ServletContext context;
    
    public void init(ServletConfig config) throws ServletException {
	    super.init(config);
        this.context = config.getServletContext();
    }
    
    public  void doGet(HttpServletRequest request, HttpServletResponse  response)
        throws IOException, ServletException {
    	
    	request.setCharacterEncoding("UTF-8");
    	
    	ArrayList tags = new ArrayList();
    	
        String nameId = request.getParameter("name");

	    if (nameId != "") {
            tags.add("<name>true</name>");
        } else {
        	tags.add("<name>false</name>");
        }
	    
	      String dataId = request.getParameter("data");
	    
	    if (dataId != "") {
	    	tags.add("<data>true</data>");
	    } else {
	    	tags.add("<data>false</data>");
        }
    	
	    if (!tags.isEmpty()) {
	    	response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write("<valid>");
            for (int i = 0; i < tags.size(); i++) {
            	response.getWriter().write(tags.get(i).toString());
			}
            response.getWriter().write("</valid>");
	    }
	    
    }

    public  void doPost(HttpServletRequest request, HttpServletResponse  response)
        throws IOException, ServletException {
    	
	    context.getRequestDispatcher("/uploadedOK.jsp").forward(request, response);
	    	
}
This is of course a sample of easy validation, but there will be a more complex validation on the server side and maybe the javaScript validation on the client side. I want to use the Ajax approach for the form validation. If there are some errors, some text will be updated on the page in the browser. If this approach with doGet() and doPost() is not good, I am glad to hear your advices, how to improve this. Maybe, there is the other possibility to send response from the server, without this xml tags. Please help me.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 28 2006
Added on Jun 29 2006
6 comments
710 views