Rohit Kumar

Ranch Hand
+ Follow
since Jul 19, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rohit Kumar

Hi,

I have a servlet which uploads file to server. Below is my code and is working file. Now, I want to check if the file uploading by the user is already exists. If so , I have to display a message to user "The file you are trying to upload already exists. Do you want to overwrite" with YES or NO actions.

How can I display this alert message from user.



Can anyone suggest me on this?
[ October 30, 2007: Message edited by: Ben Souther ]
14 years ago
Hello,

I have a String which is holding a file path like(C:\Documents and Settings\bkmal\Desktop\application.pdf)

I need to get only the file name like(application.pdf in the above fielPath).


String filePath = "C:\Documents and Settings\bkmal\Desktop\application.pdf";

String onlyfileName= filePath.substring((filePath.lastIndexOf("/")),filePath.length());

when I do this I get the following exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

what am I doing wrong?? any suggestions
14 years ago
Hi,

Below is my servlet code, where I can save the file locally at the given location. My problem is , I have a text box on JSP , where user enters company name in the text box. When I was trying to get the value of text box, it is throwing null.

I have searched on forums...and it says if ENCTYPE="multipart/form-data ..we cannot get the form field values..
Anyone can help on a way to get my form field.

FileItemFactory factory = new DiskFileItemFactory();
boolean isMultipart = FileUpload.isMultipartContent(request);
ServletFileUpload upload = new ServletFileUpload(factory);
List /* FileItem */ items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
String formFieldValue = request.getParameter("fileName"); /////It shows NULL value.
if (item.isFormField()) {
System.out.println("It is a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
String name = item.getFieldName();
String value = item.getString();
File cfile=new File(item.getName());

File folder;

if(saveFile.endsWith("pdf")){
folder = new File("C:/upload/documents");
}
else{
folder = new File("C:/upload/images");
}
File tosave=new File(folder,cfile.getName());

MyJSP:

<TR border="0">
<TD >File Location <input type="file" name="uploadFile" id="uploadFile" ></TD>
</TR>
<TR border="0">
<TD>File Name <input type="text" name="fileName" > <input type="SUBMIT" value="Upload" ></TD>
<TD></TD>

</TR>
14 years ago
Hi Shailesh and Reddy,


Thanks for the help and ypur valuable time. I used generator class as Identity and it throwed error and didnt allowed me login to application.

public RunwayTrans() {
session = HibernateUtil.getInstance().getSession();
//session.beginTransaction();
}

I have session.beginTransaction(); in my action class , which is causing the problem. I commented this, changed generator class to identity ...it works fine now. Able to login to application and able to insert records in Runway table.

-Thanks
Hi Shailesh,

I tried with differnet generator classes(Identity, native) , but it is still throwing the same error. It is failing even to login my application.

Any other suggestions...

Thanks for your time on this.
Hi shailesh,

Thanks for the reply. When i add insert and update attributes to Column tag , it is showing error(like dotted red line under Insert and Update..when i put cursor on the top it shows - Attribute "insert" must be declared for element type "column").

<hibernate-mapping>
<class name="hdpt.Runway" table="RUNWAY" schema="dbo" catalog="Aviation">
<id name="runwayId" type="integer" >
<column name="RUNWAY_ID" insert="false" update="false"/>// RED DOTTED LINE SHOWS UNDER insert and update
<generator class="assigned" />
</id>
<many-to-one name="facility" class="hdpt.Facility" fetch="select" cascade="none" >
<column name="FACILITY_ID" not-null="true" />
</many-to-one>
Can you please tell me what am I doing wrong.

-Rohit
Hi Shailesh,

I am using sql server and Runway_Id is the identity column.

when i add column attributes insert=false and update=false to facility column with generator class (Runway table)back to assigned, i was able to login my application.
<hibernate-mapping>
<class name="hdpt.Runway" table="RUNWAY" schema="dbo" catalog="Employ">
<id name="runwayId" type="integer" >
<column name="RUNWAY_ID" />
<generator class="assigned" />
</id>
<many-to-one name="facility" class="hdpt..Facility" fetch="select" cascade="none" insert="false" update = "false"> ///Added column attributes INSERT and UPDATE
<column name="FACILITY_ID" not-null="true" />
</many-to-one>



But when I try to add a new to runway table, it is throwing the following error. Because of the following error, i have changed generator class to "native" which is throwing the error that posted in my first post.

ids for this class must be manually assigned before calling save(): hdpt.Runway
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): hdtp..Runway
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at hdpt.trans.RunwayTrans.addCommonRunway(RunwayTrans.java:962)
at hdpt.model.AddRunway.update(AddRunway.java:50)
at hdpt.command.UpdateCommand.execute(UpdateCommand.java:27)
at gov.gdot.ooit.asm.control.ASMController.control(ASMController.java:93)
at gov.gdot.ooit.asm.control.ASMController.clientIDSelected(ASMController.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at javax.faces.component.UIInput.broadcast(UIInput.java:492)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:364)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:81)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3214)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
gov.gdot.ooit.asm.exceptions.ASMException: An exception has occurred when accessing database, Please contact Administrator
at gov.gdot.ooit.asm.trans.RunwayTrans.addCommonRunway(RunwayTrans.java:971)
at gov.gdot.ooit.asm.model.AddRunway.update(AddRunway.java:50)
at gov.gdot.ooit.asm.command.UpdateCommand.execute(UpdateCommand.java:27)
at gov.gdot.ooit.asm.control.ASMController.control(ASMController.java:93)
at gov.gdot.ooit.asm.control.ASMController.clientIDSelected(ASMController.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at javax.faces.component.UIInput.broadcast(UIInput.java:492)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:364)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:81)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3214)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
gov.gdot.ooit.asm.exceptions.ASMException
at gov.gdot.ooit.asm.model.AddRunway.update(AddRunway.java:56)
at gov.gdot.ooit.asm.command.UpdateCommand.execute(UpdateCommand.java:27)
at gov.gdot.ooit.asm.control.ASMController.control(ASMController.java:93)
at gov.gdot.ooit.asm.control.ASMController.clientIDSelected(ASMController.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at javax.faces.component.UIInput.broadcast(UIInput.java:492)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:364)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:81)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

Any suggestions, so that I can add new records to Runway table
hi,

I am getting this error while logging into my application.

I have 2 tables. Facility and Runway. One FACILITY can have many RUNWAY.

Below is Runway.hbm.xml file:

<hibernate-mapping>
<class name="hdpt.Runway" table="RUNWAY" schema="dbo" catalog="Employ">
<id name="runwayId" type="integer">
<column name="RUNWAY_ID" />
<generator class="assigned" /> //CHANGED TO NATIVE
</id>
<many-to-one name="facility" class="hdpt.Facility" fetch="select" cascade="none" >
<column name="FACILITY_ID" not-null="true" />
</many-to-one>

generator class for Runway is "assigned". I need to add a new record to Runway table and it throws "ids cannot be assigned manually".

So i changed the generator class to "native" in Runway table . When i try to login to application itself after changing the generator class,

it is throwing the below error.

javax.faces.FacesException: #{controller.control}: javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:78)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
Truncated. see log file for complete stacktrace
javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:130)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
Truncated. see log file for complete stacktrace
java.lang.ExceptionInInitializerError
at gov.gdot.ooit.asm.util.HibernateUtil.<clinit>(HibernateUtil.java:22)
at gov.gdot.ooit.asm.trans.LoginTransaction.begin(LoginTransaction.java:21)
at gov.gdot.ooit.asm.model.Login.logIn(Login.java:43)
at gov.gdot.ooit.asm.command.SignInCommand.execute(SignInCommand.java:28)
at gov.gdot.ooit.asm.control.ASMController.control(ASMController.java:91)
Truncated. see log file for complete stacktrace
org.hibernate.InstantiationException: could not instantiate test objecthdpt..Runway
at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:25)
at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:114)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:424)
Truncated. see log file for complete stacktrace
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:22)
Truncated. see log file for complete stacktrace


Any suggestions. why is failing to login to application itself after changing the generator class of Runway.
Hi Ben,

It is not even going to while loop..

currently our server is down..I will post you the content in log file right I am good with our server.

Thanks for your time Ben..
15 years ago
JSP
Hey Ben,

I deleted cache, recompiled..and restarted. It is still getting the null value. I have no clue why it is showing null still.


Any suggestions??
Thanks,
15 years ago
JSP
oops...it is not getting the value that I printed int he form.
15 years ago
JSP
Hi Ben,

Thanks for your time. The problem is even after removing the space, i am still getting null value. It is getting the value that I printed in the form.

Any other sugestions.

Next time, i will see that I put the code in UBB tags.
15 years ago
JSP
I am not using Struts. I am calling a servlet from this JSP and so is Action attribute in my form.

Below is my complete form

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Login</title>
</head>

<body>

<center>

<form name="userLogin" ACTION="UserValidation" METHOD="POST">

<tr> User Name
<td><input type="text" name=�userName" id="userName" ></td>
</tr>
<tr> Employer ID
<td><input type="text" name=�empId" id="empId" ></td>
</tr>
<tr>
<td><input type="SUBMIT" value="Submit" ></td>
</tr>

</form>


</center>
</body>
</html>
15 years ago
JSP
Hi,
I have a form with text box. When the user enters Name and clicks button, am calling a servlet.
What I need is �when the user enters name and clicks Submit..i want this name to be available on Servlet. I am doing the following way..but still it is showing null.
Can anyone help .
<form name="userLogin" ACTION="UserValidation" METHOD="POST">

<tr> User Name
<td><input type="text" name=�userName" id="userName" ></td>
</tr>
This is how I am getting in Servlet

userName is showing null here. How could I get the userName that is entered to form??


Thanks

[BPSouther: Added code tags]
[ September 11, 2007: Message edited by: Ben Souther ]
15 years ago
JSP
Hi ,

I have this java class to upload files to a location on my local machine. when I used the same code with JSP it is wporking fine. If I use the same code with java class it is showing exceptions fpr request object like request cannot be resolved.
request.getContentType();
request.getInputStream()

Do i need to import anything.Do please suggest me





package oosurance.trans;

import java.io.*;


public class FileUpload{

String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
//String folder = "C:/upload/images/";
File folder = new File("C:/upload/images");
if (! folder.exists()) folder.mkdirs();

FileOutputStream fileOut = new FileOutputStream( folder.getCanonicalPath() + File.separatorChar + saveFile);

//out.print("Saved here: " + saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();


}}}
15 years ago