RSS

Category Archives: Example

Apache -The system cannot find the registry key for service ‘tomcat7’ error and solution

After the installation of tomcat7, when i click on “tomcat7w”, apache tomcat service is not started and it display following error:

Solution to these error is below :
1) go to the bin folder of your tomcat and run the following command through command line :

service.bat install
you will see the message “tomcat7 service installed”

2) Now run tomcat7w.exe through the command line and it should run successfully.

and

1)Use tomcat7w.exe to always start as an administrator.

2)Right click on the tomcat7w.exe which is in ‘bin’ folder of the tomcat installation.

3)Select ‘Properties’, then in ‘Compatibility’ tab under ‘Privilege Level’.

4)Select ‘Run this program as an administrator’.

Hope this helps thanks.

 
Leave a comment

Posted by on June 15, 2017 in Example

 

Solution of program asked in elluminati

class Program 
{
	public static void main(String args[])
	{
		int n=Integer.parseInt(args[0]);
		int s=0;
		for(int i=1;i<=n;i++)
		{
			s=s+i;
		}
		int d=n-1;
		
		int t=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=i;j++)
			{
				if(j==1)
				{
					t=s;
					s--;
				}
				else
				{
					t=t-d;
					d=d-1;
				}
				System.out.print(t + " ");
			}
			System.out.println("\n");
			d=n-1;
		}
	}
}

program

 
Leave a comment

Posted by on September 28, 2016 in Example

 

Example to generate JPG image

<%@ page import="java.awt.image.*,java.io.*,java.awt.*,javax.imageio.*" %>
<%
try
{
	response.setContentType("image/jpeg");
	OutputStream o=response.getOutputStream();
	BufferedImage obj =new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
	Graphics2D g=(Graphics2D)obj.getGraphics();
	g.drawString("test",50,50);
	ImageIO.write(obj,"jpg",o);
	
}
catch(Exception e)
{
	System.out.println(e);
}

%>
 
Leave a comment

Posted by on April 6, 2016 in Example

 

Example of Programatic Security

<%@ page import="sun.misc.*" %>
<%! 
public void m1(HttpServletResponse res)
{
	res.setStatus(401);
	res.setHeader("WWW-Authenticate","BASIC realmname=enter username and password");
}
%>
<% 
	String s=request.getHeader("Authorization");
	if(s !=null)
	{
		String encodeddata =s.substring(6);

		BASE64Decoder obj=new BASE64Decoder();
		String up=new String(obj.decodeBuffer(encodeddata));

		String [] arr=up.split(":");
		if(arr[0].equals("ad") && arr[1].equals("ad") ) 
		{
			out.println("Welcome to the page" );
		}
		else
		{
			m1(response);
		}
	}
	else
	{
			m1(response);
	}
%>

 
Leave a comment

Posted by on April 5, 2016 in Example

 
Image

EL implicit Object

implicit Objects of EL

 
Leave a comment

Posted by on March 28, 2016 in Example

 

JSP EL – access to request parameters, cookies, and other request data

inputdemo.html

<html>
<body>
<form action="collection.jsp">
Enter your Name: 
<input type="text" name="nm" />
<select name="hobby" multiple >
	<option value="dance" > Dance </option>
	<option value="reading" > Reading </option>
	<option value="music" > Music  </option>
</select>
<input type="submit" value="ok"/>
</form>
</body>
</html>

collection.jsp

<body bgcolor="${(10>8)? 'pink' : 'YELLOW'}" >
EL implicit Object : <br>
==================
<br>Your name is : 
${param.nm} <br>
your hobbies are : 
${paramValues.hobby[0]}
${paramValues.hobby[1]}
${paramValues.hobby[2]} <br>

pageContext object :  <br>

Session : ${pageContext.session.id}  <br>
Method : ${pageContext.request.method} <br>

initParam : 
<!-- 
in web.xml 
<context-param>
  <param-name>AppID</param-name>
  <param-value>123</param-value>
  </context-param>
-->
${initParam.AppID}


<br>
Header : 
${header.Accept} <br> OR <br>
${header["Accept"]}  <br>
Cookie : 
${cookie.JSESSIONID.value}

<%
 pageContext.setAttribute("name", "mca");
 %>
 <br> Page Scope : 
 ${pageScope.name}

 
Leave a comment

Posted by on March 28, 2016 in Example

 

JSP EL – Accessing Collection

EL\WEB-INF\classes\CollectionDemo.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class CollectionDemo extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
    {
        String weekdays[] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
         
        List cl = new ArrayList();
        cl.add("Rajkot");
        cl.add("Surat");
        cl.add("Ahmedabad");
        cl.add("Morbi");
		      
        Map student = new HashMap();
        student.put("fnm","Ram");
        student.put("lnm","Sita");
            
        HttpSession session = req.getSession();
        session.setAttribute ("wd",weekdays);
        session.setAttribute ("city",cl);
        session.setAttribute ("stud",student);
         
        RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/CollectionDemo.jsp");
        rd.forward(req,res);
    }
}

EL\WEB-INF\CollectionDemo.jsp

${wd[0]}<br>
${city[0]} ${city[1]}<br>
${stud["fnm"]} ${stud["lnm"]} 
 
Leave a comment

Posted by on March 22, 2016 in Example

 

JSP EL – Accessing Bean SubProperty

EL\WEB-INF\source\NameBean.java

package p1;
public class NameBean
{
	private String firstname="Missing first name";
	private String lastname="Missing last name";
	
	public NameBean(String firstName, String lastName) 
	{
		setFirstName(firstName);
		setLastName(lastName);
    }
	public void setFirstName(String fn)
	{
		firstname=fn;
	}
	public void setLastName(String ln)
	{
		lastname=ln;
	}
	public String getFirstName()
	{
		return firstname;
	}
	public String getLastName()
	{
		return lastname;
	}
}

EL\WEB-INF\source\CompanyBean.java

package p1;
public class CompanyBean
{
	private String companyname,business;
	public CompanyBean(String companyName, String business) 
	{
		setCompanyName(companyName);
		setBusiness(business);
	}
	public void setCompanyName(String newCompanyName) 
	{
		companyname = newCompanyName;
	}
	public void setBusiness(String newBusiness) 
	{
		business = newBusiness;
	}
	public String getCompanyName() 
	{ 
		return(companyname); 
	}
	public String getBusiness() 
	{ 
		return(business);
	}	
}

EL\WEB-INF\source\EmployeeBean.java

package p1;
public class EmployeeBean 
{
	private  NameBean name;
	private CompanyBean company;
	public EmployeeBean(NameBean name, CompanyBean company) 
	{
		setName(name);
		setCompany(company);
	}
	public void setName(NameBean obj)
	{
		name=obj;
	}
	public void setCompany(CompanyBean obj)
	{
		company=obj;
	}
	
	public NameBean getName()
	{
		return name;
	}
	public CompanyBean getCompany()
	{
		return company;
	}
	
	
}

EL\WEB-INF\source\Test.java

package p1;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Test extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
	{
		NameBean nobj=new NameBean("Marty","Hall");
		CompanyBean cobj=new CompanyBean("coreservlet.com","J2EE Training and consulting");
		
		EmployeeBean eobj=new EmployeeBean(nobj,cobj);
		
		request.setAttribute("employee", eobj);
		request.setAttribute("company",cobj);
		request.setAttribute("name",nobj);
	
		RequestDispatcher dispatcher = request.getRequestDispatcher("/EL.jsp");
		dispatcher.forward(request, response);

	}
}

EL\EL.jsp

FirstName :  ${employee.name.firstName}
LastName :  ${employee.name.lastName}
Company Name : ${ employee.company.companyName} 
Business :  ${ employee.company.business}
<br>
From NameBean :  <br>
${name.firstName}
${name.lastName} <br>
From CompanyBean <br>
${company.companyName}
${company.business}
 
Leave a comment

Posted by on March 22, 2016 in Example

 

JSP EL : Accessing JSP Scoped Variable

EL\WEB-INF\classes\ScopeVar.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ScopeVar extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		
		req.setAttribute("attribute1","request");
		
		HttpSession session=req.getSession();
		session.setAttribute("attribute2","session");
		
		ServletContext  a=getServletContext();
		a.setAttribute("attribute3","application");
		
		
		req.setAttribute("repeat","request1");
		session.setAttribute("repeat","session1");
		a.setAttribute("repeat","application1");
		
		
		RequestDispatcher rd=req.getRequestDispatcher("result.jsp");
		rd.forward(req,res);

	}
}

EL\result.jsp

Request  scope : ${attribute1}
Session scope  : ${attribute2}
Application scope  : ${attribute3}
	<br>
	
<%-- If Key are same then priority(scope) will be as per following order :
1) page
2) request
3) session
4) application	 --%>
same Key : 	
	${repeat}
<br>	
to access same key from different Scope : 

	${requestScope.repeat}
	${sessionScope.repeat}
	${applicationScope.repeat}

 
Leave a comment

Posted by on March 22, 2016 in Example

 

MVC Example -3

StudMVC\datainput.html

<html>
	<body>
		<form action="test.do" method="post">
			Roll No:
			<select name="rno">
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
			</select> 
			<input type="submit" value="submit">
		</form>
	</body>
</html>

StudMVC\WEB-INF\web.xml

<web-app ....>
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
	<servlet>
	<servlet-name>t</servlet-name>
		<servlet-class>controller.control</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>t</servlet-name>
		<url-pattern>/test.do</url-pattern>
	</servlet-mapping>
</web-app>

StudMVC\WEB-INF\source\control.java

package controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import model.*;

public class control extends HttpServlet
{
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		
		String rno=req.getParameter("rno");
		
		
		Bean b=new Bean();
		b=b.getDetail(rno);
		
		int total=b.getM1()+b.getM2() + b.getM3();
		out.println("total " + total);
		double per=total/3;
		
		req.setAttribute("Result",b);
		req.setAttribute("total",total);
		req.setAttribute("per",per);
		
		
		String URL;
		if(per <50 )
			URL="WEB-INF/LessResult.jsp";
		else
			URL="WEB-INF/MoreResult.jsp";
		
		RequestDispatcher rd=req.getRequestDispatcher(URL);
		rd.forward(req,res); 
		
	}
}

StudMVC\WEB-INF\MoreResult.jsp

<body bgcolor="BLUE"> 

Student Data : 
<jsp:useBean id="Result" type="model.Bean" scope="request" />
Roll no : <jsp:getProperty name="Result" property="rollno" /> </br>
Name : <jsp:getProperty name="Result" property="name" /> </br>
M1 : <jsp:getProperty name="Result" property="m1" /> </br>
M2 : <jsp:getProperty name="Result" property="m2" /> </br>
M3 : <jsp:getProperty name="Result" property="m3" /> </br>

<%
	out.println("Total  : "+request.getAttribute("total") + "<br>");
	out.println("Percentage : " +request.getAttribute("per"));
%>
</body>

StudMVC\WEB-INF\LessResult.jsp

<body bgcolor="RED"> 

Student Data : 
<jsp:useBean id="Result" type="model.Bean" scope="request" />
Roll no : <jsp:getProperty name="Result" property="rollno" /> </br>
Name : <jsp:getProperty name="Result" property="name" /> </br>
M1 : <jsp:getProperty name="Result" property="m1" /> </br>
M2 : <jsp:getProperty name="Result" property="m2" /> </br>
M3 : <jsp:getProperty name="Result" property="m3" /> </br>

<%
	out.println("Total  : "+request.getAttribute("total") + "<br>");
	out.println("Percentage : " +request.getAttribute("per"));
%>
</body>

StudMVC\WEB-INF\source\Bean.java

package model;
import java.util.*;
public class Bean
{
		private int rno;
		private String name;
		private int m1,m2,m3;

		public Bean()
		{
			
		}
		public Bean(int rollno,String name,int m1,int m2,int m3)
		{
			
			rno=rollno;
			this.name=name;
			this.m1=m1;
			this.m2=m2;
			this.m3=m3;
		}
		
		public Bean getDetail(String key)
		{
				
			HashMap mp=new HashMap();
			mp.put("1",new Bean(1,"Ram",76,86,87));
			mp.put("2",new Bean(2,"Sita",52,65,73));
			mp.put("3",new Bean(3,"Laxman",65,75,53));
			mp.put("4",new Bean(4,"Hanuman",32,35,23));
			mp.put("5",new Bean(5,"Bharat",42,55,63));
			
			return ((Bean)mp.get(key));
		}
		public int getRollno()
		{
			return rno;
		}
		public String getName()
		{
			return name;
		}
		public int getM1()
		{
			return m1;
		}
		public int getM2()
		{
			return m2;
		}
		public int getM3()
		{
			return m3;
		}
}
 
Leave a comment

Posted by on March 21, 2016 in Example