织女 发表于 2009-2-24 10:37:31

可返回站点的根路径request.getContextPath()

package com.v512.webproject2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFormServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");


out.print("<form method=\"post\" action=\" " request.getContextPath() "/servlet/loginForm\" >");


out.println("用户名:<input type='text' name='username' size='18'>" "<br>");
out.println("密 码:<input type='password' name='password' size='20'>" "<br>");
out.println("<input type='submit' name='submit' value='提交'>");
out.print("<input type='reset' name='reset' value='重置'>");
out.print("</form>");
out.println("request.getContextPath""用来活动web应用的名字webprject2" "<br>");
out.println("用post的提交方式提交到自己的post" "<br>");
out.println("解决乱码request.setCharacterEncoding(\"utf-8\")" "<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.print("用户名:" request.getParameter("username") "<br>");
out.print("密 码:" request.getParameter("password") "<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

}




www.ad119.cn/bbs/attachments/basic/20090224/200922410364775077801.jpg
页: [1]
查看完整版本: 可返回站点的根路径request.getContextPath()