JSP and POJO


Hello Buddies


A quick tutorial this time, for buddies not comfortable with the concepts of MVC and Servlets, and want to access Java classes with JSPs.

For your reference -

https://github.com/namitsharma99/jspPojoExample.git



1st JSPs -

myForm.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>Enter the form details here</title>
</head>
<body>
<form action="processForm.jsp">
<label>Enter the info</label>
<input type="text" name="info">
<input type="submit" value="Submit!">
</form>
</body>
</html>



POJO class -

MyPojo.java


package com.java;

public class MyPojo {

private String info;

public String getInfo() {
System.out.println("Getter called!");
return info;
}

public void setInfo(String info) {
System.out.println("Setter called!");
this.info = info;
}

}



2nd JSP -

processForm.jsp 


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.java.MyPojo"%>
<!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>Form Processed!!</title>
</head>
<body>
<jsp:useBean id="myBean" class="com.java.MyPojo">
<jsp:setProperty name="myBean" property="info" />
<jsp:getProperty name="myBean" property="info" />
</jsp:useBean>
</body>
</html>


Run the form jsp on the localhost server and ENJOY the output.

http://localhost:xxxx/jspAndPojo/myForm.jsp

>>

http://localhost:xxxx/jspAndPojo/processForm.jsp?info=hello


>> hello

No comments:

Post a Comment

Featured post

Oracle SQL Scheduled Jobs - An Interesting Approach

  Oracle SQL Scheduled Jobs A DB Scheduler is the best way to automate any backend database job. For instance, if you want to process the p...