I have the following line of code in my jsp:
<body>
These are the names of the companies you have searched for:
<c:forEach items="${cname}" var="company">
<br>
${company}
<br>
</c:forEach>
</body>
It gives me the following output on the web page:
company1
company2
company3
I want to click on company1 and want "company 1" displayed on console, however it is just null.
I used the following method in the servlet:
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
dispatcher=req.getRequestDispatcher("/shareinfo.jsp");
System.out.println(getServletContext().getAttribute("company"));
dispatcher.forward(req, resp);
}
What is the problem?
Related
I have followed many tutorials.
the login page (index.jsp) opens, but when I submit it doesn't redirect (go to welcome page (welcome.jsp)) and gives error 404
im new to using servlets as you can see, any help will be appreciated
login class
#WebServlet(name = "login" ,urlPatterns = "/login")
public class Login extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("index.jsp");
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String password = req.getParameter("password");
if (name.equalsIgnoreCase("ahmad") && password.equalsIgnoreCase("ahmad")) {
resp.sendRedirect("welcome.jsp");
} else {
resp.sendRedirect("index.jsp");
}
}
}
index.jsp
<html>
<head>
<title>login</title>
</head>
<body>
<form action = "/login" method = "post">
Name : <input name = "name" type = "text" />
Password : <input name = "password" type = "password" />
<input type = "submit" />
</form>
</body>
</html>
welcome.jsp
<html>
<head>
<title>welcome</title>
</head>
<body>
<p><font color="red">welcome</font></p>
</body>
</html>
Error Description:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Don't work servlet to .jsp connection.
I have doPost in servlet:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF8");
getUser(req, resp);
}
private void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getParameter("id");
//In future implements database worker this for check
User user = new User(1, "test", "test", "test", new Timestamp(System.currentTimeMillis()));
req.setAttribute("user", user);
req.getRequestDispatcher("user.jsp").forward(req, resp);
}
And user.jsp file which use User object for view on web page:
<h1>User view</h1><br />
<ul>
<% User user = (User) request.getSession().getAttribute("user"); %>
<li>Id: <% user.getId(); %></li>
<li>Name: <% user.getName(); %></li>
<li>Login: <% user.getLogin(); %></li>
<li>Email: <% user.getEmail(); %></li>
<li>Create date: <% user.getCreateAccount(); %></li>
</ul><br />
menu
But it's don't work. On user's web-page all information is empty. All block <ul> don't view. I see only href to menu. And i don't understand why. Please tell me what wrong? Why? How correct it? Thank You!
In Servlet you are putting User object into request but in Jsp you are looking it up from session.
Try this in your JSP:
User user = (User) request.getAttribute("user");
I think that your jsp have some syntax error.
e.g:<% user.getId(); %>. the proper syntax : <%=user.getId()%>.
You can try it:
<% User user = (User) request.getSession().getAttribute("user"); %>
<ul>
<li>Id: <%=user.getId()%></li>
<li>Name: <%=user.getName()%></li>
<li>Login: <%=user.getLogin()%></li>
<li>Email: <%=user.getEmail()%></li>
<li>Create date: <%=user.getCreateAccount()%></li>
</ul><br/>
menu
I want to pass two values to a servlet from a jsp page. 1- a result set value, 2- a get parameter value.
It should look something like this in jsp page:
SCcalculator.getValue(rs.getString("value1"),request.getParameter("value1"));
on the servlet side how can i receive and manipulate this data in the SCcalculator package? any suggestions please?
In your servlet class use :
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String s = request.getParameter("value1");
String s1 = request.getParameter("value");
}
in JSP don't do like that, instead do :
<form action="name" method="post"> //here action=name is name of your servlet class name
<input type="text" name="value">
<input type="text" name="value1">
<input type="submit" value="Send">
</form>
Conside this example
<html>
<head>
<title>Demo application</title></head>
<body>
<form id = "form1" method = "GET" action = "../Sample Application">
link1 : <input type = "text" id = "nRequests" name = "nRequests" />
<input type = "submit" name = "submit" id = "submit"/>
</form></body></html>
Now how you servlet will accept the request
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
nRequests = request.getParameter("nRequests");
In this way you can get the value from a HTML page to your servlets.
I have a form on a jsp which calls a javascript function which latter calls a servlet. However the code mention below works once in while and when the code does reach the servlet the parameters return null. Also its really bizarre but it jumps between The doGet and doPost method even though i specify "POST". Can someone assist me with the correction.
JAVASCRIPT:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$().ready(function() {
$('.my_button').click(function() {
var dataf = 'email=' + $('#email').val()
+ '&password=' + $('#password').val();
$.ajax({
url: "http://localhost:8080/RetailerGui/loginServlet",
type: "POST",
data: dataf,
dataType: "JSON",
success: function(data) {
alert(data);
}
});
});
});
</script>
JSP FORM:
<form id="newsletter" method="POST">
<div class="wrapper">
<div class="bg">
Email:<input type="text" id="email">
</div>
<div class="bg">
Password:<input type="password" id="password">
</div>
<button class="my_button" name="login" >login</button>
</div>
</form>
SERVLET "loginServlet":
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String email = request.getParameter("email");
String password = request.getParameter("password");
String loginResult = login(email,password);
System.out.println("EMAIL:" +email);
System.out.println("PASSWORD:" +password);
System.out.println("IM INSIDE GET!!!!");
response.getWriter().write(loginResult);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String email = request.getParameter("email");
String password = request.getParameter("password");
String loginResult = login(email,password);
System.out.println("IM INSIDE POST!!!!");
response.getWriter().write(loginResult);
}
If any other information is required please let me know. Thank you for the help in advance.
I Found the solution through this site, thank you for the help everyone!
http://coderlearner.com/Java_Servlet_doGet_Ajax_JSON#LoginJSON.java
Three points here,
In the html, your button will end up submitting your form to the current url (Refer https://stackoverflow.com/a/9824845/1304559)
Set type="button" to change that. type="submit" is the default (as specified by the HTML recommendation).
In the html, <form>, add onsubmit="return false". Just to be entirely safe that it does not post back to the current url.
One question - what does processRequest do in your servlet code?
I have doPost in a servlet -
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getSession().setAttribute("sysMsg","now we gonna to break line \n");
// forward to printLine.jsp page
dispather.forward(request, response) ;
}
And jsp page (says , printLine.jsp) -
<html>
<head>
<title></title>
</head>
<body>
<font size="30" color="Red">${sysMsg} </font>
</body>
</html>
I want that in printLine.jsp the printing of sysMsg finally break line ... For that in put \n in the end of sysMsg when set him in the servlet , but this way didn't work .
Try <br/> instead
request.getSession().setAttribute("sysMsg","now we gonna to break line <br/>");