1

I am developing a simple Struts2 application having AJAX call. I am trying to check each field of a form, while it is being filled up, using AJAX call. I want to get the response from a servlet. To call the servlet, I have used 'exclude url' in struts.xml file and included the ajax call pattern in web.xml file. But my application is unable to find out the servlet. Below are the code snippets:

empForm.jsp:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="util.js" type="text/javascript"></script>
<script src="validate.js" type="text/javascript"></script>
<title>Employee Registration Form</title>
</head>

<body>
    <h1 align="center">Employee Registration Form</h1>
    <h2 align="left">Please provide your details in the appropriate fields. Fields marked with * are mandatory. </h2><br><br>
    <s:form action="register">
        <s:textfield id="username" name="username" label="Username*" />
        <s:textfield name="fullname" label="Full Name*" />
        <s:textfield name="emailid" label="Email*" />

validate.js:

window.onload = initPage;
alert("validate.js page");

function initPage() {
  document.getElementById("username").onblur = checkUsername;
  //document.getElementById("register").disabled = true;
  alert("in initPage();");
}

function checkUsername() {
    request=createRequest();
    if(request==null)
        alert("Unable to create request!");
    else{
        var theName = document.getElementById("username").value;
        var username = escape(theName);
        var url = "http://localhost:8080/PeopleManagement/checkName.do?username=" + username;
        //var url = "checkName.do?username=" + username;
        request.onreadystatechange = showUsernameStatus;
        request.open("GET",url,true);
        request.send(null);
        alert("url:" + url);
    }
    alert("in checkUsername();");
}

function showUsernameStatus(){
    if(request.readyState == 4){
        if(request.status == 200){
            alert("Status:" + request.statusText);
            alert("Response:" + request.responseText);
            if(request.statusText == "okay"){
                alert("allowed");
            }
            else
                alert("denied");
        }
    }
}

struts.xml:

<package name="admin" extends="struts-default">
    <action name="Welcome">
        <result>/welcome.jsp</result>
    </action>
    <action name="Admin" class="com.admin.AdminUser" method="execute">
        <result name="input">/adminLogin.jsp</result>
        <result name="SUCCESS">/adminMenu.jsp</result>
    </action>
</package>
<package name="registration" extends="struts-default">
    <action name="register" class="com.emp.Employee" method="execute">
        <result name="input">empForm.jsp</result>
        <result name="error">/registrationError.jsp</result>
        <result name="SUCCESS">/registrationSuccess.jsp</result>
    </action>
    <action name="viewemp" class="com.emp.ViewEMP" method="execute">
        <result name="error">/viewError.jsp</result>
        <result name="SUCCESS">/list.jsp</result>
    </action>
</package>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>PeopleManagement</display-name>
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>

  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
  </filter>

  <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>UserValidator</servlet-name>
    <servlet-class>com.validation.ValidateUser</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UserValidator</servlet-name>
    <url-pattern>/checkName.do*</url-pattern>
  </servlet-mapping>

</web-app>

please help me out.

  • 1
    Servlet? Servlet in S2 app? Servlet in S2 app to validate something? Are you sure? – Aleksandr M Feb 11 '15 at 09:22
  • If you are using Struts2 framework, you don't need to write a servlet. Instead you can use the framework's features to write actions. – Roman C Feb 11 '15 at 11:58
  • 1
    Also, which version of S2 are you using? Your filter mapping indicates you're using a pretty old one, which isn't recommended. As Aleksandr said, using a servlet for this is pretty suspicious. – Dave Newton Feb 11 '15 at 14:20
  • @AleksandrM I know there are option in framework. But, just for testing purpose, I am using the servlet ... But I am facing the problem of maping ... – NPException Feb 11 '15 at 15:58

1 Answers1

2

You need to map your Servlet correctly in web.xml

<servlet-mapping>
  <servlet-name>UserValidator</servlet-name>
  <url-pattern>/PeopleManagement/checkName.do</url-pattern>
</servlet-mapping>

.........

[EDIT] You will also have to edit /* to / for the struts interceptor servlet. The difference is explained here - Difference between / and /* in servlet mapping url pattern

Community
  • 1
  • 1
ramp
  • 1,256
  • 8
  • 14