I'm using SpringToolSuite with Java and I'm trying to render an index.jsp page onto my localhost:8080, but my Localhost is only going to the Whitelabel error page again & again. I guess it doesn't seem to be recognizing my Homecontroller.java file. The servers are running and have been refreshed. My files are correct. I see no issues anywhere else. What could be wrong that I'm not seeing? How can I render my page?
HomeController.java :
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/your_server")
public class HomeController {
#RequestMapping("")
public String index(HttpSession session) {
if (session.isNew()) {
session.setAttribute("counter", 0);
}
Integer count = (Integer) session.getAttribute("counter");
count += 1;
session.setAttribute("counter", count);
return "index.jsp";
}
#RequestMapping("/counter")
public String showCounter(HttpSession session, Model model) {
if (session.isNew()) {
session.setAttribute("counter", 0);
}
model.addAttribute("counter", session.getAttribute("counter"));
return "counter.jsp";
}
#RequestMapping("/reset")
public String resetCounter(HttpSession session) {
session.invalidate();
return "forward:/";
}
}
CounterApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CounterApplication {
public static void main(String[] args) {
SpringApplication.run(CounterApplication.class, args);
}
}
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Welcome! Want to count some stuff?</title>
</head>
<body>
How many times has this page been visited?
<p>Oh, hi.</p>
</body>
</html>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:out value="${2+2}"/>
counter.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>How many times are we going to go through this?!</title>
</head>
<body>
<c:out value="${ counter }" />
Go back.
Reset the counter.
</body>
</html>
Related
I am new to Spring MVC and Servlets. I am trying to run home() method in controller class SearchController but the output is only from home.jsp file. Statement:
out.println("<h1> this is my response block</h1>");
in not included in the result on browser.
Is there anything that can be done to print the out object statement in the browser with home.jsp file?
package springmvcsearch;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class SearchController {
#RequestMapping("/home")
public String home(HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1> this is my response block</h1>");
return "home";
}
}
This is home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
</body>
</html>
This is the output on the browser:
out.println() is used in jsp file. So you can edit your home.jsp as follows
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
<%
out.println("<h1> this is my response block</h1>");
%>
</body>
</html>
If you want to add something for home.jsp via the controller class, you can change your controller class as follows.
package springmvcsearch;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class SearchController {
#RequestMapping("/home")
public String home(HttpServletRequest req,HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String message = "this is my response block";
req.setAttribute("message", message);
return "home";
}
}
Then change your jsp file as follows.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
<h1>${message}</h1>
</body>
</html>
For further explanation, go through this link how to message from servlet and display in jsp
I'm using custom.jsp in order to avoid the login screen and load the home screen directly, but still it's asking for login credentials after restarting the tomcat. The path of the custom.jsp file is "SAP BusinessObjects\tomcat\webapps\BOE\WEB-INF\eclipse\plugins\webpath.InfoView\web"
I don't know what I'm missing here and I'm new to this. Below is my jsp code. Thanks.
<%# page import="com.crystaldecisions.sdk.exception.SDKException" %>
<%# page import="com.crystaldecisions.sdk.framework.*" %>
<%# page import="com.crystaldecisions.sdk.occa.infostore.*" %>
<%# page import="com.crystaldecisions.sdk.occa.security.*"%>
<%# page import="java.net.*"%>
<%# page import="com.crystaldecisions.enterprise.*"%>
<%# page import="com.crystaldecisions.sdk.plugin.admin.*"%>
<%# page import="java.sql.*"%>
<%# page import="com.businessobjects.webutil.Encoder" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
IEnterpriseSession enterpriseSession;
final String BO_CMS_NAME = "BI98svr";
final String BO_AUTH_TYPE = "secEnterprise";
final String BO_USERNAME = "user121";
final String BO_PASSWORD = "pass121";
ILogonTokenMgr logonTokenMgr;
String defaultToken = "";
boolean loggedIn = true;
try {
enterpriseSession =
CrystalEnterprise.getSessionMgr().logon(BO_USERNAME,BO_PASSWORD,
BO_CMS_NAME,BO_AUTH_TYPE);
logonTokenMgr = enterpriseSession.getLogonTokenMgr();
defaultToken = logonTokenMgr.createWCAToken("", 20, 1);
response.sendRedirect("http://"+BO_CMS_NAME+":8080/BOE/BI/logon/start.do?ivsLogonToken="+Encoder.encodeURL(defaultToken));
}
catch (Exception error)
{
loggedIn = false;
out.println(error);
}
%>
<!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>
</body>
</html>
So to be more specific what Im trying to solve. How can I show the first element in my Arraylist:"sortedDomainList" in my jsp?
EDIT : Model and ArrayList working tried in normal java application.
MODEL short version.
public ArrayList<String> sortedDomainList = new ArrayList<String>();
public ArrayList<String> getSortedDomainList() {
return sortedDomainList;
}
public void setSortedDomainList(ArrayList<String> sortedDomainList) {
this.sortedDomainList = sortedDomainList;
}
}
CONTROLLER/SERVLET
package com.comparebet.controller;
import com.comparebet.model.*;
#WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controller() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ArrayList<String> sortedDomainList = new BeanModel().getSortedDomainList();
request.setAttribute("d", sortedDomainList);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
}
}
VIEW/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="${pageContext.request.contextPath}/Comparebet" method="get" accept-charset="UTF-8"></form>
<h1> ${d.sortedDomainList[0]}</h1>
</body>
</html>
This is my first try on MVC so I feel really lost when it comes down to servlet.
<%# 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>
<%
ArrayList<String> sortedDomainList= request.getAttribute("d");
for(String domain:sortedDomainList){
%>
<h1> <%= domain %> </h1>
<% } %>
</body>
</html>
I am trying to print some data from my database in Mysql using some classes in java and jsp. It's my first time doing this. I googled a lot but nothing helped me. Here is my code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form action="DB_results.jsp">
<input type = "submit" value = "Database">
</form>
</body>
</html>
Draw.java
package DB_draw;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Draw {
ResultSet myRs;
public ResultSet getMyRs() {
return myRs;
}
public void setMyRs() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tst","root","");
Statement myStmt = myConn.createStatement();
this.myRs = myStmt.executeQuery("select * from tst.people");
while (this.myRs.next())
{
System.out.println(this.myRs.getString("onoma") + " " + this.myRs.getString("epitheto") + " " + this.myRs.getInt("id"));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
DB_results.jsp
<%#page import="java.sql.ResultSet"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%# page import="DB_draw.*" %>
<jsp:useBean id="DB_draw" class="DB_draw.Draw" scope="session" />
<jsp:setProperty name="DB_draw" property="*"/>
<!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>Database</title>
</head>
<body style="background-color:black">
<div style = "color:yellow; font-family:fantasy; font-size:30px">
<%
ResultSet myRs = DB_draw.getMyRs();
if(myRs == null) out.println("Problem");
else
while(myRs.next())
{
out.println(myRs.getString("onoma") + " " + myRs.getString("epitheto") + " " + myRs.getInt("id"));
}
%>
</div>
</body>
</html>
The Problem is that when i try to print the results in DB_results.jsp using variable myRs, myRs == NULL. Why is this happening? I'm using Tomcat-Apache 8.0.
You never call setMyRS(). And even if you did, setMyRS() iterates through the resultset, so myRs.next() would always return false in the JSP.
I am new to Struts 2 and trying to do use fileUpload interceptor. I am attaching all my code layers
Action Class (FileUploadAction):
package com.caveofprogramming.actions;
import java.io.File;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
#Action( value = "/fileUpload",
results={#Result(name="success",location="/success.jsp"),
#Result(name="error",location="/error.jsp"),
#Result(name="input",location="/error.jsp")
},
interceptorRefs={
#InterceptorRef(
params={"allowedTypes","image/jpeg,image/jpg,application/zip",
"maximumSize","1024000"},
value="fileUpload"
),
#InterceptorRef("defaultStack"),
#InterceptorRef("validation")
}
)
public String execute(){
try{
return SUCCESS;
} catch(Exception e){
return ERROR;
}
}
public String display() {
return NONE;
}
}
error.jsp:
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:fielderror/>
</body>
</html>
Success.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">
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success
</body>
</html>
fileUpload.jsp:
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<body>
<h1>Struts 2 <s:file> file upload example</h1>
<s:form method="post" enctype="multipart/form-data" action="fileUpload">
<s:file label="File One" name="fileUpload" />
<s:submit />
</s:form>
</body>
</html>
I am not understanding why I am getting this error
"Content-Type not allowed: fileUpload "photography-104a.jpg" "upload_37fbf440_169b_4687_af65_93c8c967256c_00000000.tmp" image/pjpeg"
Although my uploading file format is .jpg.
You are getting this error probably because you don't allow files with content type image/pjpeg. Use parameter of fileUpload interceptor to define allowed MIME types
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/pjpeg</param>
</interceptor-ref>