Skip to Main Content

Java Programming

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!

How to reload a JSP with request.getAttribute values

908351Jan 1 2012 — edited Jan 2 2012
I have this application where i want to populate a text file on the basis of entries entered from user interface. I chose Struts1 for this and i have been able to complete most of the functionalities.But the part of keeping on populating the text file on the basis of user entries in my JSP is something i am struggling with. The following are the flow of pages on user interface 1.'Accept user entries' http://www.image-share.com/ijpg-1178-104.html 2. 'Ask for scan data on the basis of entries in page1' http://www.image-share.com/ijpg-1178-105.html 3.'Submit after entering the scandata. ' http://www.image-share.com/ijpg-1178-106.html

Now as we can see in the submit on step three, the values populated on the page become null. I suppose this is due to the request.setAttribute i am using in its action. Even post validation for empty entries in the scan data text box the validation message is seen with null entries like this http://www.image-share.com/ijpg-1178-107.html . My questions are: 1. What can be done so that the values persist on the JSP page. 2. What should be used so that there is a scenario that the users enter the Scan Data on page 2 and can continue to enter more scan data values by falling back on the same JSP . I was thinking on the lines of reloading the page using JavaScript on the button click. Is it the right approach?


The relevant code for this is welcome.jsp - the page number 2 with the scan data text box
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%> 
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 <html>  
 <head>      
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">    
 <title>Production Audit Module</title>  
 
 <script language="JavaScript">

	function m() {         
	alert("Test");
	}
 </script>
 
 </head>   
 <body> 
 <h1 align="center">Production Audit Module</h1> 
  <html:form action="txtwriter">  	
    <% String itemname = (String)request.getAttribute("itemname");     %>
    <% String lotnumber = (String)request.getAttribute("lotnumber");   %>
    <% String godownname = (String)request.getAttribute("godownname");  %>
    <br/>
    
 <% String message = (String)request.getAttribute("message");   
   session.setAttribute( "theFileName", message );

 %>  
 Filename : <%= message %>
  <br/>
 Item Name :<%= itemname %>
  <br/>
 Lot Number :<%= lotnumber %>
  <br/>  
 Godown Name :<%= godownname %>
   <br/> 
   
 <bean:message key="label.scandata"/>  
  <html:text property="scanData" ></html:text>  
		 <html:errors property="scanData"/>  
	 <br/> 
  	 	 	
   
    
  	<html:submit/>  
	 <br/>   
  </html:form>   
 </body> 
 </html>
LoginAction.java - Action class for the first page
package com.store.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  
import com.store.form.LoginForm;
import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; 
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;


public class LoginAction extends Action
{    
	public ActionForward execute(ActionMapping mapping, ActionForm form,  HttpServletRequest request, HttpServletResponse response)  
	throws Exception {   
		String target = null;  
		String textFile = "";
		String dateTime ="";
		LoginForm loginForm = (LoginForm)form;   
		String[] tempDateStore;
		String delimiter = "AD at";
		String finalDate = "";
		String driveName = "";
		String folderName = "";
		String completePath = "";
		String fileNamePath = "";
		FileOutputStream fout;
		FileInputStream fin;
		if((!loginForm.getItemName().equals("")) && (!loginForm.getLotNumber().equals(""))&& (!loginForm.getStockGodown().equals(""))) 
		{        
				//Create name of txt file
				dateTime = now("yyyy.MM.dd G 'at' hh:mm:ss z");	
				tempDateStore = dateTime.split(delimiter);	
				String dateMod =tempDateStore[0].replace(".", "."); 				
				String timeMod = tempDateStore[1].replace(":", ".");
				
				finalDate = dateMod.trim()+"_" + timeMod.trim(); 
				textFile = loginForm.getItemName().trim()+"_"+loginForm.getLotNumber().trim()+"_"+loginForm.getStockGodown().trim()+"_"+finalDate+".txt";
			
				//Create a text file in C:temp after folder check
					ServletContext context = getServlet().getServletContext(); 
					driveName = context.getInitParameter("drive");
					folderName = context.getInitParameter("foldername");
					
					completePath = driveName.trim() +":\\"+ folderName.trim();
					boolean exists = (new File(completePath)).exists();
						if(!exists){
						File f = new File(completePath);
						f.mkdir();
						}
					fileNamePath = 	completePath.trim()+"\\"+textFile.trim();
					boolean fileExists = (new File(fileNamePath).exists());
					if(!fileExists){
						File file = new File(fileNamePath);
						file.createNewFile();
						
					}
						
	
				target = "success";     
				request.setAttribute("message", textFile);  
				request.setAttribute("itemname",loginForm.getItemName().trim());
				request.setAttribute("lotnumber",loginForm.getLotNumber().trim());
				request.setAttribute("godownname",loginForm.getStockGodown().trim());
			}     
		else {     
			target = "failure";    
			}     
		return mapping.findForward(target); 
		} 
	
	
	public static String now(String dateFormat) {
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
	    return sdf.format(cal.getTime());

	  }

	

	
	}
TextWriter.java the action class for the welcome.jsp
package com.store.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  
import com.store.form.TextWriter;
import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; 
import java.util.Calendar;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class TextWriterAction extends Action
{    
	public ActionForward execute(ActionMapping mapping, ActionForm form,  HttpServletRequest request, HttpServletResponse response)  
	throws Exception {   
		String completePath ="";
		String target = null;  
		TextWriter txtWriter = (TextWriter)form;   
		ServletContext context = getServlet().getServletContext(); 
		if((!txtWriter.getScanData().equals("")) && (!txtWriter.getScanData().equals(null))) 
		{        
			String name = (String)request.getSession().getAttribute("theFileName");
			String driveName = context.getInitParameter("drive");
			String folderName = context.getInitParameter("foldername");
			completePath = driveName.trim() +":\\"+ folderName.trim()+"\\"+name;
			File scanDataFile = new File(completePath);
			try {
			    BufferedWriter out = new BufferedWriter(new FileWriter(scanDataFile));
			    out.write(txtWriter.getScanData()+",1");
			    out.newLine();
			    out.close();
			} catch (IOException e) {
			}

			
			
			
			
			//System.out.println("yess" + txtWriter.getTxtFileName().trim());		
			target = "successRead";     
		//	request.setAttribute("message", textFile);  
		//	request.setAttribute("itemname",loginForm.getItemName().trim());
		//	request.setAttribute("lotnumber",loginForm.getLotNumber().trim());
		//	request.setAttribute("godownname",loginForm.getStockGodown().trim());
			}     
		else {     
			target = "failureRead";    
			}     
		return mapping.findForward(target); 
		} 
	


	

	
	}
Would like to have your valuable suggestions.Thanks

Edited by: 905348 on Jan 1, 2012 7:15 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2012
Added on Jan 1 2012
1 comment
2,027 views