JSP Tutorial #29 - Read HTML Form Data with Servlets - Part 1

preview_player
Показать описание
FULL COURSE: JSP, Servlets and JDBC (80+ videos)

----

This JSP tutorial series will help you quickly get up to speed with JSP.

----

Closed-Captioning and English subtitles available for this JSP Tutorial.

----

Follow luv2code for more JSP tutorial:

---

If you liked my JSP tutorial, then join my mailing list: Get exclusive access to new Java tutorials.

---

Questions or problems about this JSP tutorial? Post them in the comments section below.

---

Want to suggest a video for my JSP tutorial? Leave a comment below. I'm always looking for new video ideas.

Let me know what video you'd like for me to create.

---

Premium JSP Course

Need More Details on JSP?
- See my Premium JSP and Servlets course (80+ videos)

---

JSP Tutorial Transcript

All right. Let's walk through this step by step. The first step is building the HTML form. We've seen most of this before, so we start off with our form. Now, the action equals StudentServlet, so that's the name of the servlet that we're calling. Then we set up method equals GET because we want to make use of a GET request to that servlet. Then from there, we simply set up the HTML form fields. We set up First Name field, Last Name field, and also a Submit button. Again, we've seen a lot of this stuff before. Just a basic form that we're setting up for our example.

The one thing that's different here or unique that I want to point out here is that since this form is going to send the data using method=GET, this actually calls the doGet method in our servlet. Method=GET will call the doGet method in your servlet, so you have to make sure that you override the appropriate handler method or make sure you override the doGet method.

That's basically it here for our servlet example. What we're going to do in the next video is we're going to move into eclipse. We're going to get some hands-on practice. We'll actually build this example from scratch, so I'll see you in the next video.

End of JSP tutorial transcript.
Рекомендации по теме
Комментарии
Автор

Really Enjoying your videos so far. Really appreciated. Love from India 👍👍👍

Rudra_on_road
Автор

What do you think about using freemarker (freemaker templates) ?

mxd
Автор

what is problem ? jsp dont liking to servlet, any misstake?

index.jsp

<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
{
padding: 10px;
}
div
{
width:50%;
border:1px solid blue;
border-radius: 5px;
background-color: lightpink;

}
</style>
<body>
<center> <h1><u>Login Here</u></h1></center>
<center>
<div>
<form action="Login" method="GET">
<table>
<tr>
<td>Username</td>
<td><input type="text" class="form.control" name="username" placeholder="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" class="form.control" name="password" placeholder="password"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input class="btn.btn.success" type="button" value="sumbit"></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

Login.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;

import
import
import
import
import

@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

try (PrintWriter out = response.getWriter())
{
String
String
if(_username !=null && _password !=null)
{
if(_username.equals("sekhar") && _password.equals("12345"))
{

}else
{
out.println("invalid username && password");
}

}else
{
out.println("empty username or password");
out.println("<a href=index.jsp>click</a>");

}

}
}



protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}

welcome.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome</title>
</head>
<body>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<welcome-file-list>






</welcome-file-list>
<servlet>
<description></description>



</servlet>
<servlet-mapping>


</servlet-mapping>
</web-app>

paidiramesh
Автор

what is problem ? jsp dont liking to servlet, any misstake?

index.jsp

<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
{
padding: 10px;
}
div
{
width:50%;
border:1px solid blue;
border-radius: 5px;
background-color: lightpink;

}
</style>
<body>
<center> <h1><u>Login Here</u></h1></center>
<center>
<div>
<form action="Login" method="GET">
<table>
<tr>
<td>Username</td>
<td><input type="text" class="form.control" name="username" placeholder="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" class="form.control" name="password" placeholder="password"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input class="btn.btn.success" type="button" value="sumbit"></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

Login.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;

import
import
import
import
import

@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

try (PrintWriter out = response.getWriter())
{
String
String
if(_username !=null && _password !=null)
{
if(_username.equals("sekhar") && _password.equals("12345"))
{

}else
{
out.println("invalid username && password");
}

}else
{
out.println("empty username or password");
out.println("<a href=index.jsp>click</a>");

}

}
}



protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}

welcome.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome</title>
</head>
<body>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<welcome-file-list>






</welcome-file-list>
<servlet>
<description></description>



</servlet>
<servlet-mapping>


</servlet-mapping>
</web-app>

paidiramesh
join shbcf.ru