JSP form sending data including files in servlet

I am tryig to send some data from jsp form to a servlet. All my data are type="text" except from one that is type="file". I know that if i want to send the data to my servlet, i have to use request.getParameter(...). (My data) In order not to be null i have to use method="get". But if i want to upload a file i have to use method="post". How can i pass both types to my servlet using one form? Thanks in advance!

.jsp

<form method="post" action="Servlet"> user:<input type="text" name="user"/> img<input type="file" name="img"/> </form>

Servlet.java

doPost(req,resp){ user = req.getParameter("user");//user == NULL }

OR

.jsp

<form method="get" action="Servlet"> user:<input type="text"/> img<input type="file"/> </form>

Servlet.java

doGet(req,resp){ //img not passed }

-------------Problems Reply------------

Form:

<form id="uploadForm" name="uploadForm" action="UploadServlet" method="post" enctype="multipart/form-data">
user:<input type="text" name="user"/>
img<input type="file" name="image"/>
</form>

You can still get both with a post request:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
InputStream fileContent = null;
String user = "";
for (FileItem item : items)
{
if (item.isFormField())
{
String fieldName = item.getFieldName();
String fieldValue = item.getString();
if(fieldName.equalsIgnoreCase("user"))
{
user = fieldValue;
}
}
else
{
String fieldName = item.getFieldName();
if(fieldName.equalsIgnoreCase("image"))
{
fileContent = item.getInputStream();
}
}
}
}

This is just an example of how to handle post request with file type.

Category:java Views:0 Time:2019-01-11

Related post

  • Is it possible to convert form's data to file before sending to server using Javascript/JQuery? 2012-03-26

    I have a Html form. I want to get the file containing posted form's data instead of sending raw posted data to the server. In brief, I want to decrypt the form's data client side (before sending to the server). Therefore, in man-in-the-middle scenari

  • Struts2 - jQuery Ajax forms Sends Data(POSTs) 2 times in jQuery Dialogs 2011-09-20

    I have a web application(J2EE+apache+struts2+jsp+struts2-jQuery plugin). My problem is when I want to submit a form in jQuery Dialog with struts2-jQuery ajax method, it sends the form's data 2 times quickly after each other!! but when I put it outsid

  • How to send data from blackberry to servlet 2011-06-29

    I am using below code to send data to servlet HttpConnection c = (HttpConnection)Connector.open("http://localhost:8585/resposweb/resposweb?action=create_order;deviceside=true"); c.setRequestMethod(HttpConnection.POST); c.setRequestProperty("If-Modifi

  • Transform HTML form in PHP include file to single line for feeding JQuery methods 2011-05-24

    I have a some.php with some php processing which also has: <form action="" method="post"> <input type="text" name="name" value="bleh" /> <input type="submit" name="submit" value="submit" /> </form> I also have other.php with s

  • HTML why do buttons in forms send data? 2012-03-28

    This is a very rudimentary question, but I am sure someone out there knows why. In HTML, when I make a button element by itself, and do not give it and onclick and no jQuery .click() the button will just do nothing. Perfect. But when I do this and bu

  • How to send a XML file under servlets on tomcat? 2011-04-01

    I have two web services running on my tomcat lets call them X and Y. when Y is requested by X in the end Y have a String with a XML inside and now i want to return this string of XML to X. How can i do this ? is there anyway to make a special request

  • Chrome Extension Help - Send data to file to display notification 2011-06-07

    So I am creating a Chrome Extension, and I've reached a point where I want to create a desktop notification that links to a forum thread. I'm using jQuery to retrieve data from a server, which contains the ID of the thread. Since the only way to do H

  • How do I send data between files in a Chrome Extension/Website? 2012-02-03

    So, I have a chrome extension that does various things to a site. What I would like to do is rewrite it, to make it cleaner and more relevant, so I started. I have what content script, and it is 'infuse.js' which injects another file t3.js, into the

  • include file in jsp absolute path 2011-01-18

    I have a jsp page that serves dynamic content based on URL pattern - The file just includes a header and a footer and some parameters <%@ include file="header.jsp" %><%String param1="asas";%><%@ include file="customcontent.jsp" %>

  • Send data from PHP to java application 2012-03-07

    I have a PHP application with a login page. I would like to send this username and password to my java application for authentication. Is there any idea how can send the data from PHP page to java application? Is there any suggestion that I can retri

  • make server to receieve/send data from/to a client several times(morethan once) in C 2012-03-06

    I have made a client and server application. In that when ever i am sending data to the server,the server is receiving data,but the server goes into the block state(waits to recv data still when data sending has been completed from client) then I com

  • Update div after form send Mootools 1.1 to Mootools 1.4.1 2013-07-05

    I have this Mootools 1.11 script that is updating div after the form is submited , form sends data to 'form.php' file and it returns a message like "form sent." I would like to convert it to mootools 1.4.1 Mootools 1.11 $('myform').addEvent('submit',

  • How to import xml file in servlet xml file in spring mvc 2011-02-28

    i have separate xml file which has jdbc , mysql details. Now i don't know how include file in servlet xml. i tried this but didn't worked <import resource="jdbc-context.xml"> --------------Solutions------------- The documentation says: "All loc

  • MYSQL "Sending data" shows different value each time 2011-04-01

    I am a student and trying to test the performance of different databases for a project. What I was trying to do is to generate 0-99999 (by self-join a 0-9 number table several times) and measure timing. I am surprised by my results, and am wondering

  • Sending Form Data with File Data in java 2010-03-11

    How can I upload files to server using JSP/Servlet? I tried this: <form action="upload" method="post"> <input type="text" name="description" /> <input type="file" name="file" /> <input type="submit" /> </form> However, I

  • Using a jsp form to send a string to a servlet - using setAttribute in the jsp and getAttribute in the servlet 2012-04-26

    I'm experimenting with sending data from a jsp form and calling a servlet and showing that data in the servlet. I would like to use setAttribute and getAttribute. In this jsp file I'm using setAttribute: <HTML> <HEAD> <TITLE> Multi

  • how to send data from two forms in a jsp page when u click on submit in one form 2011-03-07

    In my project i have a scenario like this if i click submit in a jsp which has two forms i have send data from two forms to destination,how can i do that,Please help me out. --------------Solutions------------- If you mean that you want to send the d

  • How to use the data retrieved from a servlet in the query statement present in a jsp file? 2011-07-02

    I am working on a project and I need to connect to a database from a jsp file. I have to use the data retrieved from a servlet in the where clause of the query. I am unable to do so correctly. Please post sample codes if possible. Part of my code is:

  • Sending data from MXML file to JSP file (and recieving) using HTTPservice 2011-07-09

    I want to retrieve data from a Database and display inside a datagrid in a Flex web application. The way I'm thinking of doing it is : Send the Query data from .mxml file to the .jsp page using HTTPService. In the .jsp, connect to the database and re

  • Send data from servlet to jsp 2013-06-27

    I tried to send a list from my servlet to a jsp page. This is the servlet code: Query q = new Query("post").addSort("time", SortDirection.DESCENDING); PreparedQuery pq = datastore.prepare(q); QueryResultList<Entity> results = pq.asQueryResultLi

  • how to read excel file using servlet jsp 2014-09-26

    I had tried to read excel file from jsp to servlet in apache tomcat. The below code has been receive excel file from apache folder after write the file and get that file.I need to dont write in apache tomcat.How to directly read excel file values. Th

  • Retaining the submitted JSP form data 2009-06-28

    I am having a web form(JSP) which submits the data to different application, hosted on different server. After submitting the form data, that application redirect back to same JSP page. Now, I want to save the entered the data. What are the different

  • How can I get files uploaded in a jsp form 2011-11-07

    I have this code in my jsp side: <body> <form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <td>Select File : </td> <td><input name="file" type="file"/> </td> </tr

  • How to call Onload function in the Main page as well as @include file inner.jsp page 2010-06-04

    I have two pages one is the main page and the another one is the inner page: Page names: main.jsp , sidebar.jsp I want to call the onload function on both of these pages. Is that possible. If yes How? Below is the code for main.jsp: <!DOCTYPE html

  • How to modify include file in jsp page without showing blank content 2011-06-21

    I am a beginner with jsp servlets, and I need some help having my page not show up blank when I make changes. I am trying to include the the code from a donate-box.jsp into the layout of the org-about-panel.jsp page, which itself is included in anoth

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.151 (s). 11 q(s)