I have a JPS page from which I want to execute a shell script placed on server:
Below code works fine and "/tmp/Sample.sh" script is getting executed on server.
Now I want to do 2 things:
1.The script is getting executed as soon as page is loaded, but i want to execute it only when button is clicked.
2.I understand that use of scriplets is discouraged, what I googled is that I should call a servlet, when button is clicked, in which i can move the java code.
I'm new to these terminologies as my primary skill is not java.
I have readed theory of servlet but , not getting how exactly i can achieve above two functionality.
Any help in achieving above two points would be greatly appreciated
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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></title>
</head>
<body>
<%
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
%>
</body>
</html>
UPDATED CODE AS PER SUGGESTIONS:
http://10.111.24.21:7001/Project_1/Test_1.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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></title>
</head>
<body>
<button onclick="location.href = 'http://10.111.24.21:7001/Project_1/JavaServletClass.java';" id="RedirectButton" > Execute</button>
</body>
</html>
http://10.111.24.21:7001/Project_1/JavaServletClass.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaServletClass extends HttpServlet
{
public void init() throws ServletException { }
private void ExampleMethod() throws IOException
{
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ExampleMethod();
out.println("<h1>" + "Method Executed" + "</h1>");
}
public void destroy() { }
}
Jsp page translates to servlet eventualy so ofc it is possible to do same thong that you did in servlet. If usage of JSP page is absolutely necessery you can do fallowing things:
Create servlet follow tutorial on Create Servlet
create method to execute command
private void ExampleMethod() {
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
}
Call method from doGet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Call commands
PrintWriter out = response.getWriter();
ExampleMethod();
out.println("<h1>" + Method Executed + "</h1>");
}
Replace scriptlet in body of jsp with:
<button onclick="location.href = 'http://localhost:8080/SERVLETNAME';"
id="RedirectButton" > Execute</button>
Replace server name, location (localhost:8080) with youre values ...
FORGET ALL OF THAT AS SERVLETS AND SCRIPTING IN HTML IS SO OLD AND OBSOLETE IT SHOULD NOT BE USED ANY MORE
Related
This question already has answers here:
How to transfer data from JSP to servlet when submitting HTML form
(4 answers)
Closed 4 years ago.
I have below a jps with below code with source and destination drop down and a button "Execute" which will call a servlet.
The servet will perform some operation based on the values selected.
JSP Code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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/>
</head>
<form action="MySourceEnv" method="POST">
<select name="SourceEnv" >
<option>10.100.10.11</option>
<option>10.100.10.12</option>
</select>
</form>
<form action="MyDestEnv" method="POST">
<select name="DestEnv" >
<option>10.100.10.11</option>
<option>10.100.10.12</option>
</select>
</form>
<body>
<button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>
</body>
</html>
Servlet Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaServletClass extends HttpServlet {
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String SourceEnvParam = request.getParameter("SourceEnv");
out.println("<h1>" + SourceEnvParam + "</h1>");
LogicMethod(SourceEnvParam);
}
private void LogicMethod(String SourceEnvParam) throws IOException {
// Some logic here
}
public void destroy() {
}
}
I'm getting the value of request.getParameter("SourceEnv") as Null when execute button is clicked and servlet is called.
What's wrong I'm doing here?
I think problem created in your html code. First of all you created two form out of your body which has two action. But you defined another another action with
<button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>.
Try writing your jsp page like this
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> name<title/>
</head>
<body>
<form action="JavaServletClass" method="GET">
<select name="SourceEnv" >
<option>10.100.10.11</option>
<option>10.100.10.12</option>
</select>
<select name="DestEnv" >
<option>10.100.10.11</option>
<option>10.100.10.12</option>
</select>
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
I am trying get data from Servlet page to jsp page but i am getting null value.
i want to get the message which is there in servlet page.
please help me in this
below is my code
servlet:
package com.project1;
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("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msg="hi i am servlet";
request.setAttribute("data",msg);
request.getRequestDispatcher("Test.jsp").forward(request, response);
}
}
This is my jsp code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<div>
<%= request.getAttribute("data") %>.
</div>
</body>
</html>
Your code is working.
servlet.
You have missed #override in doGet method.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String msg = "hi i am servlet";
request.setAttribute("data", msg);
request.getRequestDispatcher("Test.jsp").forward(request, response);
}
Test.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<div>
<%=request.getAttribute("data")%>.
</div>
</body>
</html>
Maybe add
#WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
to the top of the servlet
This question already has answers here:
Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
(6 answers)
Closed 6 years ago.
I can pass Integer, String, Float, etc.. but when I am passing my defined object (Employee) the JSP is receiving it as null.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.rahul.model.bean.*"%>
<!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>Search Result</title>
</head>
<body>
<%
Employee record = (Employee) request.getAttribute("searchResult");
out.println(record);
%>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Designation</th>
<th>Department</th>
<th>Salary</th>
</tr>
</table>
</body>
</html>
And My Controlleer doGet is:
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
EmployeeDAO dao = new EmployeeDAOImpl();
Employee result = dao.search(request.getParameter("id"));
// PrintWriter pw=response.getWriter();
// pw.println(result);
ServletContext app = getServletContext();
app.setAttribute("searchResult", result);
System.out.println("Emp= "+result);
response.sendRedirect("./searchview.jsp");
}
Try this:
GreetingsServlet.java
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;
#WebServlet("/greetings")
public class GreetingsServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String message = "Hello, World";
req.setAttribute("message", message);
RequestDispatcher dispatcher = req.getServletContext().getRequestDispatcher("/WEB-INF/jsp/greetings.jsp");
dispatcher.forward(req, resp);
}
}
greetings.jsp
<%#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>
<h1><%= request.getAttribute("message") %></h1>
</body>
</html>
This cannot work with sendRedirect though as you're basically making a round-trip between the client and server, spanning over 2 requests, not one. The first request has your parameter, but since your client does not store it, it is lost when the redirection occurs. You should forward to your JSP unless what's done by the servlet should not be executed over and over again (like a database insertion). Look here if you really need to redirect.
Im trying pass specific element from my Model to servlet to jsp.
This is working in my servlet: System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1]) When I go to http://localhost:8080/Comparebet/Controller
But I dont know what Im doing wrong since I dont get it to my jsp. All tutorials ive been watching is mostly input parameters or they put code in the JSP.
UPDATE EDIT
Tried with this in my JSP and I get "null"
<%= request.getAttribute("rank1") %>
SERVLET
#WebServlet("/Controller")
public class Controller extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ArrayList<String> sortedDomainList = new BeanModel().getSortedDomainList();
BeanModel beanModel = new BeanModel();
request.setAttribute("rank1", beanModel.getSortedDomainList().get(0).split(";")[1]);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
//TESTING SERVLET
System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1]);
System.out.println("CONTROLLER CALLED");
// System.out.println(${rank1});
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
}
}
JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>CompareBet</title>
</head>
<body>
<form action="/Controller/*" method="get">
<h1>${rank1.getSortedDomainList().get(0).split(";")[1] } </h1>
</form>
</body>
</html>
In your JSP try this, instead of rank1.getSortedDomainList().get(0).split(";")[1]
<h1>${request.rank1 } </h1>
You have already done beanModel.getSortedDomainList().get(0).split(";")[1] in your servlet and have added the result to the request scope object.
Try to replace this line in your jsp:
<h1>${rank1.getSortedDomainList().get(0).split(";")[1] } </h1>
To this:
<h1>${rank1} </h1>
In JSP you can access this data using the expression language like ${rank1}.
And take it out from the <form like this:
<form action="/Controller/*" method="get">
...
</form>
Try to use <h1><%= request.getParameter("rank1")%></h1>
I have JoinCustomerToAccountServlet servlet under src/controller -
#WebServlet("/JoinCustomerToAccountServlet")
public class JoinCustomerToAccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}
And form joinCustomerToAccount.jsp under WebContent/ActionsPages -
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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></title>
</head>
<body>
<form action="JoinCustomerToAccountServlet" method="POST">
Account join to him : <input type="text" name="account" />
<input type="submit" value="join" />
</body>
</html>
After I fill the form and press on the submit button I get
type Status report
message /MyBankProject/ActionsPages/JoinCustomerToAccountServlet
description The requested resource (/MyBankProject/ActionsPages/JoinCustomerToAccountServlet) is not available.
It looks like the form searching under his folder and not search for servlet .
Because your URL mapping is /JoinCustomerToAccountServlet and because your JSP resides inside /MyBankProject/ActionsPages, you'll have to change your form's action into this:
<form action="../JoinCustomerToAccountServlet" to make it work.