It should be simple, but i have a problem,
This is my *.jsp file
<html>
<head>
<title>Edit DataBase data</title>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<hr size="2"/>
<h2>id in DB = ?</h2>
<p>id:<input type="text" name="id" size="20" value="sdfs"></p>
<p>
<form action="/web/save" method="POST">
<input class="button" type="submit" value="submit" />
</form>
</p>
</form>
</body>
</html>
servlet looks like this
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id")
System.out.println("ID=" + id);
}
but in output ID=null
servlet was loaded by the button click on the server
You need to put the input tag in between the form tags.
For the code you put here
<form action="" method="POST">
is correct.
Check, you might have put
<form action="" method="GET">
Related
I have an problem about that taking a passcode value from user, i want to use this passcode in url path, i am using thymeleaf as template engine.
This is my controller
#RequestMapping(value = "/findEvent/{passcode}", method = RequestMethod.POST)
public String findEvent(#PathVariable("passcode") String passcode,
final RedirectAttributes redirectAttributes, Model model) {
Event event=eventService.findByPassCode(passcode);
List<Question> questions=questionService.findQuestionsByPasscode(passcode);
model.addAttribute("questions",questions);
return "questions";
}
and these is my html pages
addEvent.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<form method="post" th:action="#{/eventSave}" th:object="${eventRegister}">
Name:<br>
<input type="text" th:field="*{eventName}"><br>
Passcode:<br>
<input type="text" th:field="*{eventPasscode}"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
passcode.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="#{/findEvent/{passcode}}">
Passcode:<br>
<input type="text" th:text="*{passcode}" ><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
questions.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="col-12">
<table class="table table-bordered">
<tr>
<th>Question </th>
<th>Votes </th>
</tr>
<tr th:each="questions : ${questions}" th:object="${question}">
<td th:text="*{text} "></td>
<td th:text="*{voteValue} "></td>
<a th:href="#{/voteQuestion/{id} (id=${question.questionId})}">Vote the question</a>
</tr>
</table>
</div>
and this is my result http://localhost:8080/findEvent/%7Bpasscode%7D
If you want the form action URL to contain a value from the form itself, then you need an onSubmit handler to update the URL from the form field.
Form values are only included automatically in the body of a POST request, or as query parameters of a GET request.
Anything else you have to do with JavaScript code.
<html lang="en">
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="upload" enctype="multipart/form-data" >
File:
<input type="file" name="file" id="file" /> <br/>
Destination:
<input type="text" value="/tmp" name="destination"/>
</br>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>
I want to get all file path which is chosen via browser(input type="file") to upload it to ftp server.
I had found a lot of code snaps but just they give me file name I need absolute path such as "c:\users\filename".
Thank yo for your time.
For security reason, browsers do not provide you full/absolute path for any file.
I have a simple html form (first name and last name) once I hit submit it should give me a simple response from my java servlet that says "Hello plus first name plus last name"
I ran this using eclipse and a tomcat plugin and it works perfectly.. but when I create a war file and deploy it from tomcat.. I get the first screen.. enter first and last name.. but when I hit submit, it looks for "WelcomeScreen" nowhere in my code do i have "welcomeScreen" Below is my HTML code.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Please enter your name</title>
</head>
<body>
<form name="input" action=Hello method="get">
First name: <input type="text" name="firstname"><br> Last
name: <input type="text" name="lastname"> <input type="submit"
value="Submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Please enter your name</title>
</head>
<body>
<form name="input" action="Hello.html" method="get">
First name: <input type="text" name="firstname" /><br>
Last name: <input type="text" name="lastname" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
I wrote correct code. There were some mistakes in html like;
<form name="input" action=Hello method="get">
<!-- You've forgotten quotes -->
<!-- And also dont forget to write ".html" or Hello controller in framework like MVC -->
<form name="input" action="Hello.html" method="get">
Another one
<input type="text" name="firstname">
<!-- You've forgotten /// -->
<input type="text" name="firstname" />
And your Hello.html
<html>
<head>
<title>Title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script type="text/javascript">
var get = window.location.search.replace( "?", "" );
alert(get);
</script>
</head>
<body>
<div>Content</div>
</body>
</html>
As described in the title I need to pass data from my JSP page to my servlet.
I load data out of a database into a form of my JSP page.
Now the user should be able to change that data.
So I have to send the changed data back to my servlet to update my database.
Therefore I want to use the doPost() method in my servlet
This is my JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-language" content="de" />
<link href="../resources/css/basic.css" type="text/css" rel="stylesheet" />
<title>Edit Movie</title>
</head>
<body>
<div id="wrapper">
<h2 id="title">Edit Person</h2>
<br></br>
<br></br>
<form id="1" class="appnitro" method="post" action="">
<ul>
<li id="li_1" >
<label class="description" for="element_1">Name</label>
<div>
<input id="element_1" name="element_1" class="element text large" type="text" maxlength="255" value="${requestScope.person.name}"/>
</div>
</li>
<li id="li_2" >
<label class="description" for="element_2">Deparment</label>
<div>
<input id="element_2" name="element_2" class="element text large" type="text" maxlength="255" value="${requestScope.person.department}"/>
</div>
</li>
<li id="li_3" >
<label class="description" for="element_3">Job</label>
<div>
<input id="element_3" name="element_3" class="element text large" type="text" maxlength="255" value="${requestScope.person.job}"/>
</div>
</li>
<li id="li_4" >
<label class="description" for="element_4">Biographie</label>
<div>
<textarea id="element_4" name="element_4" class="element textarea medium">${requestScope.person.biography}</textarea>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="652973" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
</body>
</html>
And this is my Servlet without the doPost() method:
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 de.hof.university.spj.model.People;
import de.hof.university.spj.model.PeopleDAO;
public class SinglePersonEditServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private PeopleDAO peopleDao = new PeopleDAO();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String name = "id";
String value = request.getParameter(name);
int id = Integer.parseInt(value);
People people = peopleDao.getPerson(id);
request.setAttribute("person", people);
RequestDispatcher reqDispatcher = request.getRequestDispatcher("../jsp/singlePersonEdit.jsp");
reqDispatcher.forward(request, response);
}
}
After the submit button was pressed I want to send the changed
data to my servlet so I can store it in my database.
Why String name = "id";
String value = request.getParameter(name); ? I can't seem to find any input in your JSP that's name = "id" ...
In the servlet, you should have this (for example), this :
String element_1_value = request.getParameter("element_1") ;
Either you forgot the input with id name or I am missing something. In any case, this is what you need to fix within your code.
Not to mention that you forgot inserting the name of the servlet in the action attribute of the form tag, so you had this :
<form id="1" class="appnitro" method="post" action="">
Which should become this :
<form id="1" class="appnitro" method="post" action="SinglePersonEditServlet">
Finally, your action method is "post" (as shown in the above two code lines), in the piece of servlet of your question you work with doGet, you ought to put your code in doPost unless that's done, otherwise it's sufficient to call doGet inside doPost.
I am a beginner myself, so I recognize one when I see it, we all started somewhere and I would recommand you this totu or any good search about "handling form data with servlet".
Note : duplicate of this, check it out for further learning :).
Regards.
I'm trying to create a form that updates an entry in a MySQL database. The table is a users table that contains various fields related to the user. I need this page to work as an update form that takes a username that is passed to it via the previous page and pre-fills the text fields with the existing data. It's a model one application that uses a presentation, transport, and data layer. The transport layer is User.java and the data layer (that interacts with the database) is UserDB.java.
Here's the code, everything is functional except the pre-filling.
UPDATE: I've edited to reflect the changes from the answers below, I also want to note that "UserDB.getUsers()" returns an ArrayList.
<%# page language="java" contentType="text/html; charset=iso-8859-1"
pageEncoding="ISO-8859-1" import="java.util.ArrayList,beans.*,data.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//get parameters from the request
String userName = request.getParameter("userName");
ArrayList userList = UserDB.getUsers(userName);
User user = userList.get(0);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles/style.css" type="text/css" />
<title>User Admin</title>
</head>
<body>
<div id="main">
<h1>Update User</h1>
<h3>User Info</h3>
<hr>
<div id="content">
<form action="updateUser.jsp" method="get">
<p>User Name<br>
<input type="text" name="userName" size="20" value="<%=userName%>"/>
</p>
<p>Password<br>
<input type="text" name="password" size="20" value="<%=user.getPassword()%>"/>
</p>
<p>First Name<br>
<input type="text" name="firstName" size="20" value="<%=user.getFirstName()%>"/>
</p>
<p>Last Name<br>
<input type="text" name="lastName" size="20" value="<%=user.getLastName()%>"/>
</p>
<p>Email Address<br>
<input type="text" name="email" size="20" value="<%=user.getEmail()%>"/>
</p>
<p>
<input type="submit" value="Commit Update">
</p>
</form>
</div>
</div>
</body>
</html>
I know I'm doing something wrong with the "user" object, but I'm overlooking it.
you are passing the username as a string
<%
//get parameters from the request
String userName = request.getParameter("userName");
user = UserDB.getUsers("userName");
//it should be (with out the quotes
user = UserDB.getUsers(userName);
%>
The "username" should have the quotes removed. Also, where are you declaring user's type?
ie.
User user = ....getUser..