我正在尝试在JSP代码中使用HTTP身份验证。但是我在MyAuthenticator上收到错误无法解析为某种类型。 sytax对于我在jsp页面中写入的代码是否正确。任何建议将不胜感激..
<%@ page language="java" import="java.net.Authenticator,java.net.PasswordAuthentication,java.io.BufferedReader,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String urlToQuery = request.getParameter("url");
System.out.println(" " +urlToQuery);
//URL url = new URL(urlToQuery);
//InputStream in = conn.getInputStream();
String urlString = "";
String username = "";
String password = "";
Authenticator.setDefault(new MyAuthenticator(username, password));
URL url = new URL(urlToQuery);
URLConnection conn = url.openConnection();
InputStream content = (InputStream) url.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("Done.");
class MyAuthenticator extends Authenticator {
private String username, password;
public MyAuthenticator(String user, String pass) {
username = user;
password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Requesting Host : " + getRequestingHost());
System.out.println("Requesting Port : " + getRequestingPort());
System.out.println("Requesting Prompt : " + getRequestingPrompt());
System.out.println("Requesting Protocol: " + getRequestingProtocol());
System.out.println("Requesting Scheme : " + getRequestingScheme());
System.out.println("Requesting Site : " + getRequestingSite());
return new PasswordAuthentication(username, password.toCharArray());
}
}
%>
<%=line %>
答案 0 :(得分:0)
不要在JSP中定义内部类。将JSP视为一种简单的方法。
JSP类似于(1):
public class MyJSP extends Servlet {
public void service(HttpRequest request, HttpResponse response) {
/** JSP CODE HERE **/
}
}
定义一个内部类应该作为一个非常内在的类来完成:
Authenticator.setDefault(new Authenticator() {
protected getPasswordAuthentication() {
System.out.println("Requesting Host : " + getRequestingHost());
System.out.println("Requesting Port : " + getRequestingPort());
...
}
});
我不知道如何传递参数(我只使用了最简单的匿名内部类)。
无论如何,对于任何将从外部方法使用的东西,我会使用公共类(在它自己的文件中)并避免所有这些麻烦。
(1)不完全是这样,但你明白了。
答案 1 :(得分:0)
<% ... %>
引导JSP将代码内容视为语句。因此,您的类成为遵循与局部变量相同的范围规则的本地类(也就是说,您必须在使用它之前声明该类)。我没有测试它,但是如果你将代码重写为:
<%@ page language="java" import="java.net.Authenticator,java.net.PasswordAuthentication,java.io.BufferedReader,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
class MyAuthenticator extends Authenticator {
private String username, password;
public MyAuthenticator(String user, String pass) {
username = user;
password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Requesting Host : " + getRequestingHost());
System.out.println("Requesting Port : " + getRequestingPort());
System.out.println("Requesting Prompt : " + getRequestingPrompt());
System.out.println("Requesting Protocol: " + getRequestingProtocol());
System.out.println("Requesting Scheme : " + getRequestingScheme());
System.out.println("Requesting Site : " + getRequestingSite());
return new PasswordAuthentication(username, password.toCharArray());
}
}
String urlToQuery = request.getParameter("url");
System.out.println(" " +urlToQuery);
//URL url = new URL(urlToQuery);
//InputStream in = conn.getInputStream();
String urlString = "";
String username = "";
String password = "";
Authenticator.setDefault(new MyAuthenticator(username, password));
URL url = new URL(urlToQuery);
URLConnection conn = url.openConnection();
InputStream content = (InputStream) url.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("Done.");
%>
<%=line %>
然后MyAuthenticator
应该可以在您的代码中解析。
考虑将Java类移动到单独的文件中,以使代码更具可读性。