cannot be resolved to a type
I am trying to use HTTP Authentication in my JSP code. But I am getting error on MyAuthenticator cannot be resolved to a type. Is the sytax correct for the code that I have writtent in jsp page. Any suggestions will be appreciated..
<%@ 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");开发者_如何学Go
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 %>
Do not define inner classes in JSPs. Consider a JSP like a simple method.
A JSP is something like(1):
public class MyJSP extends Servlet {
public void service(HttpRequest request, HttpResponse response) {
/** JSP CODE HERE **/
}
}
Defining an inner class should be done as an anonimous inner class:
Authenticator.setDefault(new Authenticator() {
protected getPasswordAuthentication() {
System.out.println("Requesting Host : " + getRequestingHost());
System.out.println("Requesting Port : " + getRequestingPort());
...
}
});
Not sure what would I do to pass parameters (I only have used the simplest anonymous inner classes).
Anyway for anything that is going to be used from outside methods, I would use a public class (in its own file) and avoid all these troubles.
(1)Not exactly this, but you get the idea.
<% ... %>
leads JSP to treat the code contents as statements. Therefore you class becomes a local class following the same scope rules as local variables (that is, you must declare the class earlier than using it). I didn't test it, but if you rewrite your code to:
<%@ 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 %>
Then MyAuthenticator
should be resolvable in your code.
Consider moving the Java class to a separate file to make your code more readable.
精彩评论