Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Form values are not set while getting the validation(fielderrors) errors in struts 2  RSS feed

dhanaraj krishnasamy
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am working with Struts 2

In that use the form with enctype="multipart/form-data" because i use fileupload. in this when get any one field error it wont display the other fields value so please help me any one...

The Following code i used in the JSP

<s:form name="signup" theme="ajax" action="signup.action" method="POST" enctype="multipart/form-data">

<div style="text-align:right; width:600px"> <strong class="strong"> * </strong> All the fields are Mandatory </div>
<dt> <span> <img src="<s:url value='/images/jobseeker_signup.png'/>" alt="recruiter" title="recruiter" border="0" style="margin:5px 0 0 20px;;"/> </span></dt>
<dd>  </dd>
<dt><strong> Login Details </strong> </dt>
<dd>  <s:actionerror/> </dd>
<dt> <s:label key="getText('signup.username')" theme="simple"></s:label> <strong class="strong"> * </strong>:</dt>

<dd> <s:textfield id="username" name="username" onchange="CheckDuplicate();" theme="simple"/>
<span class="hint"> Choose a Username. The username should be a minimum of 6 characters and a maximum of 24 characters. Do not include spaces or special characters , other than _, . , @ For e.g.: John_kingston, brad.123.<span class="hint-pointer"> </span></span>
<div id="chkusername" align="center" style="visibility:hidden;color:#2A2B7E;font-weight: bold;"></div> </dd>
<dt> <s:label key="getText('signup.password')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:password name="password" theme="simple" />
<span class="hint"> provide the password which should have minimum 6 to maximum 10 characters.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.confirmpassword')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:password name="confirmpassword" theme="simple"/>

<span class="hint"> reenter the password you already entered in the previous option for the verification purpose.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>

<dd> <s:textfield id="email" name="email" onchange="CheckEmail();" theme="simple"/>

<span class="hint"> enter your email which you have using currently.<span class="hint-pointer"> </span></span>
<div id="chkemail" align="center" style="visibility:hidden;color:#2A2B7E;font-weight: bold;"></div> </dd>
<dt> <s:label key="getText('')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="mobileno" theme="simple"/>
<span class="hint"> enter your mobile number.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.landline')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="landlineno" theme="simple"/>
<span class="hint"> provide your landline number with state code.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.industry')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:radio cssStyle="width:45px;vertical-align:center;" list="'textile'" name="industrytype" onclick="document.signup.industry.disabled=1" theme="simple"/>
<s:radio cssStyle="width:45px;" list="'others'" name="industrytype" onclick="document.signup.industry.disabled=0" theme="simple"/>

<dt> <s:label key="getText('signup.otherindustry')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="industry" disabled="1" theme="simple"/>
<span class="hint"> provide your current industry name if you chosen other industry.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.role')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="role" theme="simple"/>
<span class="hint"> enter your role or designation of your current or previous company.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.experience')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:select name="years" headerValue="-Years-" headerKey="1" list="{'0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40+'}" theme="simple"/>
<s:select name="months" headerValue="-Months-" headerKey="1" list="{'0','1','2','3','4','5','6','7','8','9','10','11'}" theme="simple"/>
<dt> <s:label key="getText('signup.upload')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:file name="resume" accept="text/plain" theme="simple"/>

<dt>  </dt>
<dd> <input type="checkbox" style="border:none; width:50px;vertical-align:middle" />I Accept the Whereismyboss Terms of use </dd>
<dt>  </dt>
<button type="submit">
<img src="<s:url value='/images/buttons/submit.png'/>" alt="submit" style="vertical-align:middle"/> </button>
<button type="submit">
<img src="<s:url value='/images/buttons/cancel.png'/>" alt="submit" style="vertical-align:middle"/> </button>

The Following code is used in Action

package login.action;
import java.sql.*;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pgcinfotech.conf.LoadDatabase;
import com.pgcinfotech.crypto.Encrypt;

import login.constants.WIMBConstants;

public class SignUpAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

private String username=getUsername();
private String password=getPassword();
private String confirmpassword=getConfirmpassword();
private String firstname=getFirstname();
private String lastname=getLastname();
private String email=getEmail();
private String mobileno=getMobileno();
private String landlineno=getLandlineno();
private String industrytype=getIndustrytype();
private String industry=getIndustry();
private String role=getRole();
private String resumeContentType=getResumeContentType();
private String resumeFileName=getResumeFileName();
private File resume=getResume();
private int years=getYears();
private int months=getMonths();
private String parm;
private String j_captcha_response;
private HttpSession session;
//private CommonCaptchaImage CommonCaptcha = new CommonCaptchaImage();
private HttpServletRequest request;
private HttpServletResponse response;

private String recaptcha_challenge_field;
private String recaptcha_response_field;
private String remote_Address;
//private ReCaptchaResponse reCaptchaResponse;
//private ReCaptchaImpl reCaptchaImpl = new ReCaptchaImpl();
//private SQLExecuter sqlExe = null;
//Connection conn=null;
//CallableStatement ct =null;
//private ResultSet rs=null;

public String execute() throws Exception {
Connection conn = null; // Connection declaration
/** is = new; os = new"g:/resume/" + resumeFileName);
byte buffer[] = new byte[8192];
int count = 0;
while((count = > 0)
os.write(buffer, 0, count);


//request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
//response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
/*parm = request.getParameter("j_captcha_response");
session = request.getSession();
j_captcha_response= (String)session.getAttribute(CommonCaptcha.CAPTCHA_KEY);
addActionError("Invalid Code! Please try again!");
return ERROR;
/* remote_Address = request.getRemoteAddr();

recaptcha_challenge_field = request.getParameter("recaptcha_challenge_field");

recaptcha_response_field = request.getParameter("recaptcha_response_field");

Checking the challenge field and response field are equal using checkAnswer()
reCaptchaResponse = reCaptchaImpl.checkAnswer(remote_Address, recaptcha_challenge_field, recaptcha_response_field);
addActionError("Invalid Recaptcha code! Please try again");
return ERROR;


String folder = ServletActionContext.getServletContext().getRealPath("/resume");

File rootDir = new File(folder);


String fileEx = resumeFileName.substring(

String fileRealName = resumeFileName.substring(0, resumeFileName.indexOf(".")) +fileEx;

String fileName = folder + "\\" + fileRealName;

System.out.println("documentFileFileName : "+resumeFileName);

//MimetypesFileTypeMap m = new MimetypesFileTypeMap();
//System.out.println("Type "+m.getContentType(documentFileContentType));
copy(resume,new File(fileName));

int randVal = new java.util.Random().nextInt();
String key = Integer.toString(randVal);
Encrypt ecr = new Encrypt();
String encrPwd = ecr.encryptText(key + "", password);
try {

conn = LoadDatabase.getInstance(WIMBConstants.DATA_SOURCE_KEY).getConnection();
CallableStatement ct=conn.prepareCall("call sp_insertjssignup(?,?,?,?,?,?,?,?,?,?,?,?,?)");

ct.setString(1, username);
ct.setString(2, encrPwd);
ct.setString(3, firstname);
ct.setString(4, lastname);
ct.setString(5, email);
ct.setString(6, mobileno);
ct.setString(7, landlineno);
// ct.setString(8, industrytype);
ct.setString(8, role);
ct.setInt(9, months);
ct.setString(10, key);
ct.setString(11, "JS");
ct.setString(12, "WEB");
ct.setString(13, resumeFileName);


return SUCCESS;
catch(Exception e) { sw = new; pw = new;
return ERROR;}
finally {
try {
} catch (SQLException e) {

//return ERROR;

public int getMonths() {
return months;
public void setMonths(int months) {
this.months = months;
public int getYears() {
return years;
public void setYears(int years) {
this.years = years;
public String getConfirmpassword() {
return confirmpassword;
public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
public String getRole() {
return role;
public void setRole(String role) {
this.role = role;
public String getEmail() {
return email;
public void setEmail(String email) { = email;
public String getIndustrytype() {
return industrytype;
public void setIndustrytype(String industrytype) {
this.industrytype = industrytype;
public String getLandlineno() {
return landlineno;
public void setLandlineno(String landlineno) {
this.landlineno = landlineno;
public String getMobileno() {
return mobileno;
public void setMobileno(String mobileno) {
this.mobileno = mobileno;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public File getResume() {
return resume;
public void setResume(File resume) {
this.resume = resume;
public String getResumeContentType() {
return resumeContentType;
public void setResumeContentType(String resumeContentType) {
this.resumeContentType = resumeContentType;
public String getResumeFileName() {
return resumeFileName;
public void setResumeFileName(String resumeFileName) {
this.resumeFileName = resumeFileName;
public String getIndustry() {
return industry;
public void setIndustry(String industry) {
this.industry = industry;

public String getFirstname() {
return firstname;
public void setFirstname(String firstname) {
this.firstname = firstname;
public String getLastname() {
return lastname;
public void setLastname(String lastname) {
this.lastname = lastname;

public String getJ_captcha_response() {
return j_captcha_response;
public void setJ_captcha_response(String j_captcha_response) {
this.j_captcha_response = j_captcha_response;
public String getParm() {
return parm;
public void setParm(String parm) {
this.parm = parm;

public void setServletRequest(HttpServletRequest request){
this.request = request;

public HttpServletRequest getServletRequest(){
return request;

public void setServletResponse(HttpServletResponse response){
this.response = response;

public HttpServletResponse getServletResponse(){
return response;

private void copy(File src, File dst){
InputStream in = null;
OutputStream out = null;
in = new BufferedInputStream( new FileInputStream(src));
out = new BufferedOutputStream( new FileOutputStream(dst));

byte [] buffer = new byte [1024];
while ( > 0 )
}catch (Exception e){




Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


First understand when you validate your form and validation called then the flow of your flow not reached at your action calss.

So all the variable which is use in jsp initialize again when your validation fail.

so, when you validate every time your validate methid call.

so Override validate methd in your Action class and initialize your hidden varibale in validate methid. Like.

public void validate() {

initialize all variable and write code what you written at your Action class


all variable which you want to get back when validation failed you have to again initialize at validate method.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!