Wednesday, 24 January 2018

Advancedjava-Slip1



Slip1

Q1. Write a java program to display IP Address and Name of client machine. 


import java.net.*;

class ClientInfo
{
public static void main(String args[])
throws UnknownHostException{
InetAddress i = InetAddress.getByName("localhost");
System.out.println(i);
System.out.println("IP Address:"+i.getHostAddress()+
"\nName:"+i.getHostName());
}
}
         
Q2. Write a JSP script to accept username, store it into the session, compare it with password in another jsp file, if username matches with password then display appropriate message in html file. 


Loginpassword.html                                                                                                                                    

<html>

<head><title>JSP Page</title></head>
<body>
<h1>Login Page</h1>
<center>
<h2>Signup Details</h2>
<form action="LoginCheck.jsp" method="post">
<br/>Username:<input type="text" name="username">
<br/>Password:<input type="password" name="password">
<br/><input type="submit" value="Submit">
</form>
</center>
</body>
</html>

LoginCheck.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head><title>JSP Page</title></head>
<body>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if((username.equals("ansh") && password.equals("amol")))
{
session.setAttribute("username",username);
response.sendRedirect("Home.jsp");
}
else
response.sendRedirect("Error.jsp");
%>
</body>
</html>

Home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="Error.jsp"%>
<html><head><title>JSP Page</title></head>
 <body>      
 <br/><br/><br/><br/><br/>
<center>
<h2>
<%
String a=session.getAttribute("username").toString();
out.println("Hello  "+a);
%>
</h2>
<br/><br/><br/><br/><br/><br/><br/>
<a href="logout.jsp">Logout</a>
</center>
</body>
</html>

 Error.jsp
%@page contentType="text/html"%>

<html>
 <head><title>JSP Page</title></head>
<body><h1>Some Error has occured,Please try again later...</h1></body>
</html>