为什么这个程序在mainservlet页面下出不来效果?
上传不了,只能发代码了
这是作为主启动项启动的
package example_3_sg;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class UserServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>UserServlet</title></head>");
out.println(
"<body bgcolor=\"#ffffff\" onload='javascript:frm.username.focus()'>");
out.println("<form method=post action='/ChatModule/mainservlet' name='frm'><center>请输入您的昵称<br><input type='text' name='username'><input type='submit' value='聊天'></center></form>");
out.println("</body>");
out.println("</html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
然后再是跳转后就是空白一片了,不知道哪里出了问题,如果需要后续代码,我会继续发上来
package example_3_sg;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MainServlet extends HttpServlet {
public MainServlet() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
if (username == null || username.equals("")) {
out.println(
"<h1 style='color:red' align='center'>请返回并输入有效的用户名</h1>");
} else {
Vector vec = (Vector)this.getServletContext().getAttribute(
"userArray");
if (vec == null) {
vec = new Vector();
}
if (!vec.contains(username)) {
vec.add(username);
HttpSession ses = request.getSession();
ses.setAttribute("username", username);
this.getServletContext().setAttribute("userArray", vec);
out.println("<html>");
out.println("<head><title>MainServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<frameset rows='80%,*'>");
out.println("<frameset src='topservlet?name=" + username + "'>");
out.println("<frameset src='messageservlet?name=" + username + "'>");
out.println("<frameset>");
out.println("</body>");
out.println("</html>");
} else {
out.println(
"<h1 style='color:red' align='center'>正在使用的用户ID</h1>");
}
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
private void jbInit() throws Exception {
}
}