{"id":195,"date":"2022-11-01T07:22:32","date_gmt":"2022-11-01T07:22:32","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet\/"},"modified":"2022-11-01T07:22:32","modified_gmt":"2022-11-01T07:22:32","slug":"solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/","title":{"rendered":"[Solved] How to receive a file type parameter from html\/jsp into a servlet"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>Receiving a file type parameter from HTML\/JSP into a servlet can be a tricky process. It requires knowledge of the servlet API, HTML, and JSP. This tutorial will provide a step-by-step guide on how to receive a file type parameter from HTML\/JSP into a servlet. It will cover the necessary steps to ensure that the file type parameter is properly received and handled. Additionally, it will provide tips and best practices to ensure that the process is as efficient and secure as possible.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>The following code snippet shows how to receive a file type parameter from HTML\/JSP into a servlet:<\/p>\n<p>\/\/ Get the file parameter from the HTML\/JSP page<br \/>\nString fileName = request.getParameter(&#8220;file&#8221;);<\/p>\n<p>\/\/ Create a File object from the file parameter<br \/>\nFile file = new File(fileName);<\/p>\n<p>\/\/ Read the file contents into a byte array<br \/>\nbyte[] fileData = Files.readAllBytes(file.toPath());<\/p>\n<p>\/\/ Set the file data as an attribute in the request<br \/>\nrequest.setAttribute(&#8220;fileData&#8221;, fileData); <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/p>\n<p><\/p>\n<div id=\"answer-36427092\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"36427092\" data-parentid=\"36408805\" data-score=\"0\" data-position-on-page=\"2\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>After painstaking efforts and google search I found a solution to my problem. A page from Stackoverflow helped very much. First I changed the get method of my form to post like this <\/p>\n<pre><code>&lt;form action=\"Upload\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    Image&lt;input type=\"file\" name=\"image\" id=\"image\" accept=\"image\/jpg\"&gt;\n    &lt;input type=\"submit\" value=\"submit\"&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>Then I wrote the following servlet code. We accept the <code>&lt;input type=\"file\"&gt;<\/code>data as Part data in servlet. Then we convert it to input stream. The input stream then can be saved in database. Here is my Servlet:-<\/p>\n<pre><code>package controller;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.PrintWriter;\nimport java.nio.file.Path;\nimport java.nio.file.Paths;\nimport java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.MultipartConfig;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport javax.servlet.http.Part;\nimport model.ConnectionManager;\n\n@MultipartConfig(location=\"\/tmp\", fileSizeThreshold=1048576, maxFileSize=20848820, maxRequestSize=418018841)\npublic class Upload extends HttpServlet {\n\n    @Override\n    protected void doPost(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n        Part filePart=request.getPart(\"image\");`\/\/ Retrieves &lt;input type=\"file\" name=\"image\"&gt;`\n        String filePath = filePart.getSubmittedFileName();\/\/Retrieves complete file name with path and directories \n        Path p = Paths.get(filePath); \/\/creates a Path object\n        String fileName = p.getFileName().toString();\/\/Retrieves file name from Path object\n        InputStream fileContent = filePart.getInputStream();\/\/converts Part data to input stream\n\n        Connection conn=ConnectionManager.getConnection();\n        int  len=(int) filePart.getSize();\n        String query = (\"insert into IMAGETABLE(ID,NAME,LENGTH,IMAGE) VALUES(?,?,?,?)\");\n\n\n        try {\n            PreparedStatement pstmt = conn.prepareStatement(query);\n            pstmt.setInt(1, 5);\n            pstmt.setString(2, fileName);\n            pstmt.setInt(3, len);\n            pstmt.setBinaryStream(4, fileContent, len);\n            success=pstmt.executeUpdate();\n        } catch (SQLException ex) {\n            System.out.println(\"Error : \"+ex.getMessage());\n        }finally{\n            try{\n                if(fileContent!=null)fileContent.close();\n                if(conn!=null)conn.close();\n            }catch(IOException | SQLException ex){\n                System.out.println(\"Error : \"+ex.getMessage());\n            }\n        }\n\n    }\n\n}\n<\/code><\/pre>\n<p>After execution, it does the job successfully. We accept the image from user and save it in database. Hope this solution will help all ?<\/p>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p> <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p>\n<\/div>\n<\/div>\n<p>solved How to receive a file type parameter from html\/jsp into a servlet <\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/div>\n<p>[ad_2]<\/p>\n<h1>Solved: How to Receive a File Type Parameter from HTML\/JSP into a Servlet<\/h1>\n<p>When developing web applications, it is often necessary to pass a file type parameter from an HTML or JSP page to a servlet. This can be a tricky process, as the servlet needs to be able to interpret the file type parameter correctly. Fortunately, there are a few simple steps that can be taken to ensure that the servlet receives the file type parameter correctly.<\/p>\n<h2>Step 1: Set the Form Encoding Type<\/h2>\n<p>The first step is to set the form encoding type to &#8220;multipart\/form-data&#8221;. This will ensure that the servlet is able to interpret the file type parameter correctly. To do this, add the following line of code to the HTML or JSP page:<\/p>\n<pre><code>&lt;form enctype=\"multipart\/form-data\"&gt;<\/code><\/pre>\n<h2>Step 2: Add the File Type Parameter<\/h2>\n<p>The next step is to add the file type parameter to the HTML or JSP page. This can be done by adding the following line of code:<\/p>\n<pre><code>&lt;input type=\"file\" name=\"file\"&gt;<\/code><\/pre>\n<p>This will create an input field that allows the user to select a file. The name of the input field should be set to &#8220;file&#8221; so that the servlet can interpret the file type parameter correctly.<\/p>\n<h2>Step 3: Retrieve the File Type Parameter in the Servlet<\/h2>\n<p>The final step is to retrieve the file type parameter in the servlet. This can be done by using the getPart() method of the HttpServletRequest object. The following code snippet shows how this can be done:<\/p>\n<pre><code>Part filePart = request.getPart(\"file\");<\/code><\/pre>\n<p>This will retrieve the file type parameter from the HTML or JSP page and store it in the filePart object. The servlet can then use this object to access the file type parameter.<\/p>\n<p>By following these steps, it is possible to pass a file type parameter from an HTML or JSP page to a servlet. This can be a tricky process, but with the right steps, it can be done successfully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] Receiving a file type parameter from HTML\/JSP into a servlet can be a tricky process. It requires knowledge of the servlet API, HTML, and JSP. This tutorial will provide a step-by-step guide on how to receive a file type parameter from HTML\/JSP into a servlet. It will cover the necessary steps to ensure &#8230; <a title=\"[Solved] How to receive a file type parameter from html\/jsp into a servlet\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\" aria-label=\"More on [Solved] How to receive a file type parameter from html\/jsp into a servlet\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] Receiving a file type parameter from HTML\/JSP into a servlet can be a tricky process. It requires knowledge of the servlet API, HTML, and JSP. This tutorial will provide a step-by-step guide on how to receive a file type parameter from HTML\/JSP into a servlet. It will cover the necessary steps to ensure ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T07:22:32+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to receive a file type parameter from html\/jsp into a servlet\",\"datePublished\":\"2022-11-01T07:22:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\"},\"wordCount\":597,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\",\"name\":\"[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T07:22:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to receive a file type parameter from html\/jsp into a servlet\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb","og_description":"Introduction [ad_1] Receiving a file type parameter from HTML\/JSP into a servlet can be a tricky process. It requires knowledge of the servlet API, HTML, and JSP. This tutorial will provide a step-by-step guide on how to receive a file type parameter from HTML\/JSP into a servlet. It will cover the necessary steps to ensure ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T07:22:32+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to receive a file type parameter from html\/jsp into a servlet","datePublished":"2022-11-01T07:22:32+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/"},"wordCount":597,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/","name":"[Solved] How to receive a file type parameter from html\/jsp into a servlet - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T07:22:32+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to receive a file type parameter from html\/jsp into a servlet"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}