JSP: Session cannot be resolved to a type
I've taken over some old legacy JSP code and wanted to add some mail support to it. Well, it didn't go so well, as I get errors, such as:
Session cannot be resolved to a type
An error occurred at line: 39 in the jsp file: /xxxxx/test.jsp
Message cannot be resolved to a type
MimeMessage cannot be resolved to a type
etc
开发者_JAVA百科the code, sample taken off somewhere (not pretty...):
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page session="true"%>
<html>
<head>
<title>JSP JavaMail Example </title>
</head>
<body>
<%
String to = "sssssss@gmail.com";
String from = "admin@yyyyyy.com";
String subject = "subject";
String messageText = "body";
Properties props = System.getProperties();
//props.put("mail.host", host);
//props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
%>
</body>
</html>
Googling indicated the actual mail jars may be missing, so I download javamail, extracted the jars and put them into the tomcat WEB-INF directory. Without a change.
This a rackhostcloud setup with CentOs (5.5?) on it. This was configured by the old team (long gone). Safe to say, I'm not going to be known as the best Linux admin around, but basic command line stuff is no problem. I can run yum
just fine, but I think there is more to it then this at this point. Perhaps some Java config?
I just want to send a simple mail...
Any suggestions, much appreciated.
The mail jars need to go in WEB-INF/lib, not in WEB-INF. You'll need to restart the app or the entire tomcat server for it to take effect.
From your description, I'm guessing you're working with an exploded webapp directory found at $TOMCAT_HOME/webapps/. First thing to be aware of: if there's a .war right next to the directory, then the war is the "source of truth". A war is a Web ARchive. It's basically a zip file containing an entire webapp directory, and Tomcat extracts that directory for performance purposes. What that means to you is that whatever you do to the directory, if that war file's timestamp gets updated somehow, it will blow away all changes you've made to the directory and replace it with the contents of the war again.
Now after that, all you should need in order to use the mail classes is a file structure like:
$TOMCAT_HOME
bin
conf
lib
...
webapps
<your app>
test.jsp (Can be in any directory under <your app> except WEB-INF)
WEB-INF
lib
mail.jar
activation.jar
I had the same problem. It is not your fault, but Tomcat's. On Tomcat6, the mail.jar will not work correctly if you put it in WEB-INF/lib folder like you'd do with any other jar. You have to put mail.jar in the common lib folder (/usr/share/tomcat6/lib), get rid of the mail.jar in the WEB-INF/lib folder, restart Tomcat6, and it'll start working. You don't need the activation jar with tomcat6 either.
Other application servers will likely work fine with the mail.jar being in WEB-INF/lib, or more likely already have their own mail libraries built in. With Tomcat6, you have to do the above.
Move this code to a Servlet and let your IDE guide organize your imports and suggest how to fix your compilation problems. This code has no place in a .jsp
Then of course double-check if WEB-INF/lib
contains all the required jars.
精彩评论