Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
Standardmäßig läuft eine Session nach 30 min. Inaktivität ab. Das kannst du z.B. mit setMaxInactiveInterval anpassen.Dann muss ich mich aber auch ums destroyen der Session kümmern und das ist doch beim "sorglosen" Schließen des Browsers nur über ein Timeout möglich, oder?
package servlets;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
/**
* Servlet implementation class Connect
*/
public class Connect extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Connect()
{
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
final String host = request.getSession().getAttribute("host").toString();
final String user = request.getSession().getAttribute("user").toString();
final String pw = request.getSession().getAttribute("pw").toString();
InputStream in = null;
OutputStream out = null;
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw.toCharArray());
}
} );
try
{
URL url = new URL("http://" + host + "/CGI/Screenshot");
in = url.openStream();
response.setContentType("image/bmp");
out = response.getOutputStream();
byte[] buffer = new byte[8000];
int nr;
while ((nr = in.read(buffer)) != -1)
{
out.write(buffer, 0, nr);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
in.close();
out.close();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
session.setAttribute("sessionID", session.getId());
session.setAttribute("host", request.getParameter("host"));
session.setAttribute("user", request.getParameter("user"));
session.setAttribute("pw", request.getParameter("pw"));
session.setAttribute("interval", request.getParameter("interval"));
ServletContext myServletContext = getServletContext();
RequestDispatcher rd = myServletContext.getRequestDispatcher("/screenshot.html");
rd.forward(request, response);
}
}
package servlets;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sun.net.www.protocol.http.AuthCacheValue;
import sun.net.www.protocol.http.AuthCacheImpl;
/**
* Servlet implementation class ValidateLogin
*/
public class ValidateLogin extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ValidateLogin()
{
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
InputStream in = null;
final String host = request.getParameter("host");
final String user = request.getParameter("user");
final String pw = request.getParameter("pw");
try
{
AuthCacheValue.setAuthCache(new AuthCacheImpl());
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw.toCharArray());
}
} );
URL url = new URL("http://" + host + "/CGI/Screenshot");
in = url.openStream();
session.setAttribute("sessionID", session.getId());
session.setAttribute("host", host);
session.setAttribute("user", user);
session.setAttribute("pw", pw);
session.setAttribute("interval", request.getParameter("interval"));
ServletContext myServletContext = getServletContext();
RequestDispatcher rd = myServletContext.getRequestDispatcher("/redirect.html");
rd.forward(request, response);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
in.close();
}
}
}