0% found this document useful (0 votes)
94 views5 pages

Language Contenttype Pageencoding Prefix Uri: "Java" "Text/Html Charset Iso-8859-1" "Iso-8859-1" "C"

This document describes the process for uploading documents in a Java web application. It includes code snippets for the JSP page that displays the upload form and list of documents, the DAO interface and implementation to save and retrieve documents, and the controller that handles the upload request and displays the response. The upload form submits a file to the controller, which saves it to the database and returns the list of all documents to the JSP on success.

Uploaded by

Venkat Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views5 pages

Language Contenttype Pageencoding Prefix Uri: "Java" "Text/Html Charset Iso-8859-1" "Iso-8859-1" "C"

This document describes the process for uploading documents in a Java web application. It includes code snippets for the JSP page that displays the upload form and list of documents, the DAO interface and implementation to save and retrieve documents, and the controller that handles the upload request and displays the response. The upload form submits a file to the controller, which saves it to the database and returns the list of all documents to the JSP on success.

Uploaded by

Venkat Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Document Upload Process Part#2

#1 JSP Code : Document.jsp

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<h2>welcome to Documents Page</h2>
<form action="upload" method="post" enctype="multipart/form-
data">
<pre>
SELECT DOCUMENT: <input type="file" name="fileOb"/>
<input type="submit" value="Upload"/>
</pre>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>LINK</th>
</tr>
<c:forEach items="${list}" var="ob">
<tr>
<td> <c:out value="${ob[0]}"/> </td>
<td> <c:out value="${ob[1]}"/> </td>
<td>
DOWNLOAD
</td>
</tr>
</c:forEach>
</table>
<br/>
${message}
</body>
</html>

IDao and DaoImpl Code:

package com.app.dao;

import java.util.List;

import com.app.model.Document;

public interface IDocumentDao {

public Integer saveDocument(Document doc);


public List<Object[]> getDocsIdAndNames();
}

package com.app.dao.impl;

import java.util.List;

import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.app.dao.IDocumentDao;
import com.app.model.Document;
@Repository
public class DocumentDaoImpl implements IDocumentDao {
@Autowired
private HibernateTemplate ht;

@Override
public Integer saveDocument(Document doc) {
return (Integer) ht.save(doc);
}

@Override
public List<Object[]> getDocsIdAndNames() {
//SQL:select fid,fname from doctab
String hql="select fileId,fileName from
com.app.model.Document";
List<Object[]> data=(List<Object[]>) ht.find(hql);
return data;
}

#3 Controller:

package com.app.controller;

import java.util.List;

import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.multipart.commons.CommonsMultipartFi
le;
import com.app.model.Document;
import com.app.service.IDocumentService;

@Controller
@RequestMapping("/docs")
public class DocumentController {
@Autowired
private IDocumentService service;

//1. show Document Page


@RequestMapping("/all")
public String showDocs(ModelMap map) {
//show all uploaded docs
List<Object[]> list=service.getDocsIdAndNames();
map.addAttribute("list",list);

return "Documents";
}

//2. on click upload

@RequestMapping(value="/upload",method=RequestMethod.POST)
public String uploadDoc(
@RequestParam("fileOb") CommonsMultipartFile
file,
ModelMap map)
{

if(file!=null) {
//convert CMF data to Model class obj
Document doc=new Document();
//doc.setFileId(fileId); auto generated
doc.setFileName(file.getOriginalFilename());
doc.setFileData(file.getBytes());
int id=service.saveDocument(doc);
map.addAttribute("message", "Saved with
Id:"+id);
}
//show all uploaded docs
List<Object[]> list=service.getDocsIdAndNames();
map.addAttribute("list",list);

return "Documents";
}

You might also like