taking String of controller to the textarea using servlet - java

I have this code
<div class="row">
<div class="col-md-10">
<form class="form-horizontal" action="ChatController">
<textarea name="bottxt" id="disabledTextInput" border="2" class="form-control col-xs-6" rows="8" cols="60"></textarea><br>
<input class="form-control" type="text" name="usertxt" placeholder="your text here">
<button type="submit" class="btn btn-success active"> Send </button>
</div>
</div>
So i have ChatController. I want to return a string every time user types something in TextBox and press "submit". How can i do that .

From what I have understood from our conversation here is your answer. First we have to convert your html page to jsp page because only jsp page can receive response send in form of request dispatcher from servlet. Here it is :-
//textView.jsp
<%#page import="model.TextBean"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div class="row">
<%
TextBean txt=new TextBean();
txt=(TextBean)request.getAttribute("txt");
String text="";
if(txt!=null && txt.getText()!=null){
text=txt.getText();
}
%>
<div class="col-md-10">
<textarea name="bottxt" id="disabledTextInput" border="2" class="form-control col-xs-6" rows="8" cols="60"><%=text%></textarea><br>
<form class="form-horizontal" action="ChatController" method="post">
<input class="form-control" type="text" name="usertxt" placeholder="your text here">
<button type="submit" class="btn btn-success active"> Send </button>
</form>
</div>
</div>
</body>
</html>
Then we receive the value sent from this page in a servlet. But first we have to design a java class called TextBean. Its text variable will store the value of text entered.
package model;
public class TextBean {
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
In our servlet we assign the value received from jsp page to this bean. Then we use request dispatcher to send response back to the jsp page in form of attribute.
package controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.TextBean;
public class ChatController extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String text=request.getParameter("usertxt");
TextBean txt=new TextBean();
txt.setText(text);
RequestDispatcher rd = request.getRequestDispatcher("textView.jsp");
request.setAttribute("txt", (TextBean)txt);
rd.forward(request, response);
}
}
In jsp page we create a new TextBean and set it to the value received from servlet. Then using getter method from bean we store the text in a string variable and then display it in textarea. If it is what you want mark the problem solved by clicking right mark in left side of my answer. If it is not let me know. Happy Coding :)

Related

Java Servlet Null attributes [duplicate]

This question already has an answer here:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 2 years ago.
I'm having a problem with my code. I'm making a dealership website. I'm getting a null value for my attribute. My program runs through a foreach loop of my ArrayList on my JSP, I also have buttons on each car to purchase them. When the user chooses purchase on that item its suppose to fetch the car that they chose and forward it to a purchase page. But for some odd reason when I get to the purchase page the car value is null. Any help will be appreciated.
Servlet:
package com.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dealership.Car;
import com.dealership.Inventory;
/**
* Servlet implementation class PurchaseServlet
*/
#WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public PurchaseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession(true);
Car purchaseCar = (Car)session.getAttribute("purchaseCar");
Inventory inventory = new Inventory();
if(purchaseCar == null) {
purchaseCar = new Car();
}
if (request.getParameter("purchase") != null) {
inventory.test(purchaseCar);
}
session.setAttribute("purchaseCar", purchaseCar);
RequestDispatcher rs = request.getRequestDispatcher("purchase.jsp");
rs.forward(request, response);
}
}
JSP:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:include page="/HomepageServlet" />
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Local CSS -->
<link rel="stylesheet" href="styles.css" />
<!-- Link to font -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
href="https://fonts.googleapis.com/css2?family=Cinzel:wght#600&display=swap"
rel="stylesheet">
<title> Tropical Auto</title>
</head>
<body>
<div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Nick's Tropical Autocenter</a> <a
class="navbar-brand" href="#"> <img src="mango.svg" width="30"
height="30" alt="" loading="lazy">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarNav" aria-controls="navbarNav"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active"><a class="nav-link"
href="index.jsp"> INVENTORY <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item"><a class="nav-link" href="usedcars.jsp">USED
INVENTORY</a></li>
<li class="nav-item"><a class="nav-link" href="specials.jsp">SPECIALS</a>
</li>
<li class="nav-item"><a class="nav-link" href="#">ABOUT US</a>
</li>
</ul>
<form class="d-flex" action="SearchCarsServlet" method="post">
<input class="form-control me-2" type="search" name="searchInput"
placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
</div>
<div class=album py-5bg-light">
<div class="container">
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
<c:forEach var="car" items="${inventory.carList}">
<c:if
test="${car.used == false && car.residence < 120 && car.purchased == false}">
<div class="col">
<div class="card" style="width: 18rem;">
<img src=${car.image } class="card-img-top" alt="...">
<div class="card-body">
<p><span class="mandatory">${car.make}${car.model}</span></p>
<p class="card-text">${car.description}
</div>
<div class="d-flex justify-content-center align-items-center">
<form action="PurchaseServlet" method="POST">
<c:set var="purchaseCar" value="${car}" scope="application" />
<button type="submit" name="purchase" class="btn btn-success">Purchase</button>
</form>
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#"
role="button" id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false"> Details </a>
<div class="dropdown-menu details-button"
aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Make: ${car.make}</a> <a
class="dropdown-item" href="#">Model: ${car.model}</a> <a
class="dropdown-item" href="#">Year: ${car.year}</a> <a
class="dropdown-item" href="#">Mileage: ${car.mileage}</a> <a
class="dropdown-item" href="#">Price: $${car.price}</a>
</div>
</div>
</div>
</div>
</div>
</c:if>
</c:forEach>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
</body>
</html>
It's a lot of code to comprehend but I would say the problem is here:
<foreach>
<form>
<c:set var="purchaseCar" value="${car}" scope="application" />
<submit...
</form>
</foreach>
Session scope will be enough here. But the real problem is this is executed for all records and each is overriding the previous one. So the purchaseCar variable is modified on each iteration.
Usually in a form you want to have <input> tags that sends the needed information with the request to the servlet. Usually that would be the car's ID :)

Go to inscription page

i'm have problem with load page by id. I'm creating simple forum in Spring and when i created topic i got page like this /topic/10 (10 is id topic) and nextly i want to add inscription to this topic so after click button submit should be /inscription/topic/10 (10 is id topic). Now is the problem, when i click button i got Error 405 but when i take this line and write by hand or copy address and paste it again then i got inscription page. Where is problem? Its can be thymeleaf or spring request?
Code is below
#Controller
#RequestMapping("/inscription/")
public class InscriptionController {
private InscriptionService inscriptionService;
private TopicService topicService;
#Autowired
private InscriptionController(InscriptionService inscriptionService, TopicService topicService) {
this.inscriptionService = inscriptionService;
this.topicService = topicService;
}
#GetMapping("topic/{id}")
public String in2(#ModelAttribute("inscription") Inscription inscription, #PathVariable Long id, Model model) {
Topic topic = topicService.findOne(id);
model.addAttribute("inscription", inscription);
return "inscription";
}
Thymeleaf - here when i press button New Message should be go to inscription/topic/id
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create new topic</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-light" style="background-color: #969bd9;">
<span class="navbar-brand">Create new topic</span>
</nav>
<div class = "container">
<div class = "row">
<div class = "col-md6 col-md-offset-3">
<form th:action="#{/inscription/topic/{id}(id = ${topic.id})}" th:object="${inscription}" method="post">
<h5>
<a th:href="#{/topic/{id}(id = topic.id)}"></a>
<span th:text="${topic.title}"></span>
</h5>
<div class="col s10">
<div class="row">
<div class="col s11">
Stworzony
<p th:text="${topic.createdAt} ? ${#calendars.format(topic.createdAt, 'HH:mm dd MMMM yyyy')}"></p>
<p th:utext="${#strings.replace(topic.text,T(java.lang.System).getProperty('line.separator'),'<br />')}"></p>
<div class="divider"></div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">New message</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
405 error means "Method Not Allowed". When you click the submit button, your form will perform the POST method, which doesn't have the corresponding mapping in your controller. You should add POST mapping to your controller or change the form method to GET.

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userData' available as request attribute

I am newbie in spring mvc. I am trying to make simple crud in spring using MySQL jdbc. For datamapping I have used spring form. Whenever I try to use spring form it shows an Error
Error is here
Whenever i remove spring form and use normal form it looks ok but shows an error when used spring form. The error says java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userData' available as request attribute
header.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">SpringCRUDDemo</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li>View</li>
<li>Update</li>
<li>Delete</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
index.jsp
<%#include file="./shared/header.jsp" %>
<div class="container">
<form:form commandName="userData" action="#" method="post" enctype="multipart/form-data">
<label for="firstname" class="label">Enter your first name</label>
<form:input path="firstName"/>
</form:form>
</div>
servlet class
package com.nishan.springcruddemo.servlet;
import com.nishan.springcruddemo.daoimp.CustomerDaoImp;
import com.nishan.springcruddemo.entity.Customer;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* #author Dell
*/
#Controller
public class DefaultController {
#Autowired
CustomerDaoImp customerDaoImp;
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "index";
}
#RequestMapping(value = "/insert", method = RequestMethod.POST)
public String save(#ModelAttribute("userData") Customer customer) {
try {
customerDaoImp.insert(customer);
} catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex.getMessage());
}
return "redirect:/index";
}
#RequestMapping(value = "/view", method = RequestMethod.GET)
public String viewCustomer() {
return "view-customer";
}
}
You have to Use #RequestParam to get single value.
To Bind form data with model class You need to create Model(POJO) as userData commandName="userData".CommandName and ModelClass Name both same.
Fields of class firstName and both same. Then Your Form Data Bind with Pojo It will come at your controller Class.
I hope it will help.

How to get values from JSP to Servlet in <option> [duplicate]

This question already has answers here:
How to transfer data from JSP to servlet when submitting HTML form
(4 answers)
Closed 5 years ago.
I'm writing a jsp file code that creates dropdown menus dynamically; the entries on the menus are dinamically inserted after a query executed in dao.QueriesDAO java class. Additionally, there is a search bar.
I want all the selected voices from the menus, plus the string inserted in the search bar, are sent to the servlet SearchServelt.java, contained in src/controller/SearchServlet.java, after the Search button it's clicked.
JSP file (in WebContent/jsp/homeView.jsp):
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.List, java.util.Iterator" %>
<!DOCTYPE html>
<html>
<head></head>
<body>
<jsp:include page="_header.jsp"></jsp:include>
<jsp:include page="_menu.jsp"></jsp:include>
<div style = "text-align: center">
<form action="/Search" method="post">
Search <input name="search"> <input type="submit" value="Search"/>
</form>
</div>
<div style = "text-align: center">
<%-- select value brand from drop-downlist --%>
<div style = "display: inline-block">
<%
List<String> brands = dao.QueriesDAO.getBrands();
Iterator<String> iterBrands = brands.iterator();
%>
<form name="f1" method="post" action="/Search">
Select brand:
<select name="brand">
<option value="All">All</option>
<% while(iterBrands.hasNext()){ %>
<option><%= (String) iterBrands.next() %></option>
<% } %>
</select>
</form>
</div>
<%-- select value of instrument type from drop-downlist --%>
<div style = "display: inline-block">
<%
List<String> instrumentTypes = dao.QueriesDAO.getInstrumentType();
Iterator<String> iterInstrumentTypes = instrumentTypes.iterator();
%>
<form name="f2" method="post" action="/Search">
Select instrument type:
<select name="instrumentType">
<option value="All">All</option>
<% while(iterInstrumentTypes.hasNext()){ %>
<option><%= (String) iterInstrumentTypes.next() %></option>
<% } %>
</select>
</form>
</div>
<%-- select value used from drop-downlist --%>
<div style = "display: inline-block">
<form name="f3" method="post" action="/Search">
Select used status:
<select name="used">
<option value="0">All</option>
<option value="false">Not used</option>
<option value="true">used</option>
</select>
</form>
</div>
<%-- select value product type from drop-downlist --%>
<div style = "display: inline-block">
<form name="f4" method="post" action="/Search">
Select product type:
<select name="productType">
<option value="All">All</option>
<option value="2">Professional product</option>
<option value="1">Scholastic product</option>
<option value="0">Classic product</option>
</select>
</form>
</div>
</div>
<jsp:include page="_footer.jsp"></jsp:include>
</body>
</html>
Servlet file:
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(urlPatterns = { "/search"})
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = -1953084286713579746L;
public SearchServlet() {
super();
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String searchParameters= request.getParameter("search");
String brandSelected= request.getParameter("brand");
String selectedInstrumentType= request.getParameter("instrumentType");
String selectedUsedStatus= request.getParameter("used");
String selectedProductType= request.getParameter("productType");
System.out.println("Inserted: " + searchParameters + ", "
+ brandSelected + ", "
+ selectedInstrumentType + ", "
+ selectedUsedStatus + ", "
+ selectedProductType + ".");
}
}
I want to be able to work with the values from the servlet and then eventually call other java methods and/or jsp files.
I don't know what is wrong, because I've seen similar questions on stackoverflow and I utilized the solution proposed.
I'll like to know what do I do of wrong and what should be a good approach to a problem like this, thank you very much.
The homeView.jsp file is called from a different servlet, HomeServlet.java. Should I use that servlet instead of a new servlet SearchServlet.java? What is better?
EDIT:
I resolved with <form action="${pageContext.request.contextPath}/search" method="get"> in the JSP page (and modified in having a single form), and accordingly I changed the SearchServlet.java method from doPost to doGet.
You need to add a form to the search input
Search <input name="search"> <input type="submit" name="submit" value="Search"/>
When the user clicks on the search button, it gets submitted to the post request of the servlet "SearchServlet". There you get the "search" parameter name which will contain the input from the user.
<form action="SearchServlet" method="post">
Search <input name="search">
<input type="submit" value="Search"/>
// here you can add other inputs like brand selected, instrumentType, productType etc...
</form>
Then from the servlet you query the database with the user input and set the results.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String brandSelected= request.getParameter("search");
//if you add more options in the form you can get those also
//query database and get arraylist of instrumentTypes by brand.
List<String> instrumentTypes = dao.QueriesDAO.getInstrumentType(brandSelected);
//set attribute for jsp
request.setAttribute("instrumentTypes", instrumentTypes);
//add the name of the jsp file you want to view the attribute you just set
RequestDispatcher rd = request.getRequestDispatcher("searchview.jsp");
rd.forward(request, response);
}
}
to view the attribute in JSP, do:
${instrumentTypes}

how to persist value in text field after refresh?

I am uploading a file in a form, that form also contains some textfields I enter some values in the textfields. I want this value to remain when I click upload button. And there is also a save button, when I click this button uploaded file should get saved in database. Can any one help me out?
JSP file is here:
<body>
<form action="./upload" enctype="multipart/form-data" >ID: <input type="text" name="id" value="" />Name: <input type="text" name="name" value="" />
<input name="uploaded" type="file" />
<input name="save" type="submit" value="Upload" />
<input type="submit" value="save1" name="save" /></form>
</body>
I need the bussiness logic in a servlet..
Your options are:
Persist the data to your back-end and re-populate the form
Persist the data to the the in-browser Storage (http://www.w3schools.com/html/html5_webstorage.asp)
Put the upload form in an IFRAME
The easiest option is the IFRAME.
Hey you are saying the text field values should be there while clicking Upload button. You don't have to do this thing. By default it will be there. You should it will venish man? Please mention your exact requirement.
See there is no provision to keep the file field data using value attribute.
See this link
package comunity.stackoverflow.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestController
*/
public class TestController extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestController() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,
HttpServletResponse response) {
storeInRequest(request, "id");
storeInRequest(request, "name");
storeInRequest(request, "uploaded");
// write your upload logic here then navigate to the same page;
try {
request.getRequestDispatcher("/test.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void storeInRequest(HttpServletRequest request,String param){
String val = request.getParameter(param);
if(param!=null && !param.isEmpty()){
System.out.println(val);
request.setAttribute(param, val);
}
}
}
Use standard.jat and jstl.jar
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<form action="upload.do" enctype="multipart/form-data" >
ID: <input type="text" name="id" value="${id}"/>
Name: <input type="text" name="name" value="${name}" />
<input name="uploaded" type="file" />
<input name="save" type="submit" value="Upload" />
<input type="submit" value="save1" name="save" /></form>
</body>
</html>
Use this JSP file. It might solve your problem.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<form action="upload.do" enctype="multipart/form-data" >
ID: <input type="text" name="id" value="${id}"/><br/>
Name: <input type="text" name="name" value="${name}" /><br/>
<input type="file" id="selectedFile" style="display: none;" />
<input type="text" name="uploaded" id="uploaded" value="${uploaded}" readonly="readonly" size="60">
<input type="button" value="Browse..." onclick="mymethod()" /><br/>
<input name="save" type="submit" value="Upload" />
<input type="submit" value="save1" name="save" /></form>
</body>
<script type="text/javascript">
function mymethod(){
document.getElementById('selectedFile').click();
var val = document.getElementById('selectedFile').value;
document.getElementById('uploaded').value = val;
}
</script>
</html>

Categories

Resources