Redirecting to a Servlet from a Jsp on Button Click

Redirecting to a servlet from a JSP on button click

you have to write

<form action=/your_servlet_page_name>

And you have to use

<input type="submit" value="confirm" name="conf"/>

And also you have to map your servlet page into web.xml file like

<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>

Redirecting to a servlet from a JSP on button click

you have to write

<form action=/your_servlet_page_name>

And you have to use

<input type="submit" value="confirm" name="conf"/>

And also you have to map your servlet page into web.xml file like

<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>

How to redirect to servlet when we have multiple buttons

in form : give name to buttons

First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>


<input type="button" value="Insert" name="button">



<input type="button" value="Update" name="button">



<input type="button" value="Delete" name="button">
</form>

String button_param = request.getParameter("button");
RequestDispatcher rd = null;

if(button_param .equals("Insert")
{
rd=request.getRequestDispatcher("InsertServlet");
}
else if(button_param .equals("Update"))
{
rd=request.getRequestDispatcher("UpdateServlet");
}
else if(button_param .equals("Delete"))
{
rd=request.getRequestDispatcher("DeleteServlet");
}

rd.forward(request, response);

Change jsp on button click

You have several options, I'll start from the easiest:

1- Change the input buttons to links, you can style them with css so they look like buttons:

<a href="CreateCourse.jsp">Creazione Nuovo Corso</a>

instead of

<input type="button" value="Creazione Nuovo Corso" name="CreateCourse" />

2- Use javascript to change the action of the form depending on the button you click:

<input type="button" value="Creazione Nuovo Corso" name="CreateCourse" 
onclick="document.forms[0].action = 'CreateCourse.jsp'; return true;" />

3- Use a servlet or JSP to handle the request and redirect or forward to the appropriate JSP page.

Redirect to jsp page from another jsp page through servlet when button is clicked?

You would need a parameter called source in your <form>. You may add this parameter as a hidden:

<form action="<%=request.getContextPath()%>/UserController" name="login" method="post">
<!-- current content... -->
<input type="hidden" name="source "value="customerLogin" />
</form>

Then your requestSource variable will be filled with this parameter.

Still, note that this isn't a good approach and you should not rely on hidden fields to support navigation. Define the navigation through specific data for the user like role (obtained in the process of user authentication) or another.

How to redirect the previous page in JSP via servlet

My answer

<script type="text/javascript">

$(document).ready(function() {

$("#buttonCancel").click(function() {
history.go(-1);
});
});

</script>

How can i send data from one jsp to other pressing a button

There are two ways that i know of that you can use to do this with.

1: surround with form

You can surround that table in userlist with a form, then have hidden inputs. There's a button for each form, and whatever button the user clicks, is what variables will be sent.

 <c:forEach var="obj" items="${list}" varStatus="count">
<form action="GetUser" method="get">
<div class="listinfo"> <img src="${pageContext.request.contextPath}/images/imageholder.jpg" alt="" class="listingimage" />
<div>
<table>
<tr>
<td width="10%" class="label">Nickname</td>
<td id="nickname" class="text longfield">
nickname: <c:out value="${obj.nickname}"></c:out>
</td>
</tr>

<tr>
<td width="10%" class="label">Email</td>
<td id="email" class="text longfield" >
email: <c:out value="${obj.email}"></c:out>
</td>
</tr>

<tr>
<td width="10%" class="label">Phone Number</td>
<td id="phoneNumber" class="text longfield">
<c:out value="${obj.phoneNumber}"></c:out>
</td>
</tr>

</table>

</div>


</div>
<input type="hidden" name="nickname" value="${obj.nickname}">
<input type="hidden" name="email" value="${obj.email}">
<input type="hidden" name="phoneNumber" value="${obj.phoneNumber}">
<div>
<button type="submit">View Details</button>
</div>

<div class="clear"> </div>
</form>
</c:forEach>

1: submit via link

The other option is to use an anchor tag with a href attribute that sends the values to your GetUser servlet. Note: this will only work with GET requests. Because GET requests are mapped to your servlet url (check web.xml). POST requests can't do this.
Also note: "../" might not be required in your setup. If for example the user is at the url website.com/users/randomName. Then it will be required.

for example:

     <c:forEach var="obj" items="${list}" varStatus="count">

<a href="../GetUser?nickname="${obj.nickname}"&email="${obj.email}"&phoneNumber="${obj.phoneNumber}">${obj.nickname}</a>

</c:forEach>

Nice tip on viewing what gets sent:
right click on the page and click inspect, if you go on the network heading, here you can see what gets sent to your servlet. It's useful for debugging.
enter image description here

The Servlet:

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;

public class GetUser extends HttpServlet {
private static final long serialVersionUID = 1L;

public GetUser() {
super();
}

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

//get the values submitted to servlet
String nickName = request.getParameter("nickname");
String email = request.getParameter("email");
String number = request.getParameter("phoneNumber");

//set the values that will be accessed in userprofile
request.setAttribute("name", nickName);
request.setAttribute("email", email);
request.setAttribute("number", number);


RequestDispatcher rd=request.getRequestDispatcher("Userprofile.jsp");
rd.forward(request,response);


}

}

To access those variables in the userprofile jsp, simply:

${name}
${email}
${number}

Hope that helps. Feel free to ask anything.



Related Topics



Leave a reply



Submit