我有一个包含 doPost() 和 doGet() 方法的 java servlet 3.0。两种方法在调用时都连接到同一个端口。 问题:有没有办法在方法之外使用 SSL,以便每次请求进入时,servlet 通过 (SSL) 连接到端口然后调用适当的方法?我是 Java 新手,仍在学习课程。请多多包涵。
//imports
@WebServlet("/addUser")
@MultipartConfig(fileSizeThreshold=1024*1024*3, // 3 MB
maxFileSize=1024*1024*40, // 40 MB
maxRequestSize=1024*1024*50
)
public class addUser extends HttpServlet {
private static final long serialVersionUID = 1L;
private SSLSocketFactory sslsocketfactory;
OutputStream kmipoutstream;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet Hit..");
System.out.println("trying to contact *** on port ***");
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(
"************", ****);
sslsocket.startHandshake();
kmipoutstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
kmipoutstream);
final String path = request.getPathInfo();
String all_Users = "GET ALL USERS";
// doing stuff here
};
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("*************************");
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(
"******************", ***);
sslsocket.startHandshake();
kmipoutstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
kmipoutstream);
if (!(request.getPathInfo()==null)){
String path = request.getPathInfo();
if(path.endsWith("/update")){
// doing stuff
} } }};
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
try {
// doing stuff here too
}