初识java web
关于用java调用实现web网页
1 下图为目标效果
先设置网页的子路径,保证网页能到此页面
@WebServlet("/login") //先设置网页路径
设置StringBuilder可变字符串行数,设置其中内容为html代码。
private void showMsg(HttpServletResponse resp,String aa) throws IOException {
resp.setCharacterEncoding("UTF-8");//设置该网页的编码为utf-8
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("<html>");
stringBuilder.append("<header>");
stringBuilder.append("<title>test</title>");
stringBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");
stringBuilder.append("</header>");
stringBuilder.append("<body>");
stringBuilder.append(aa);
stringBuilder.append("</body>");
stringBuilder.append("</html>");
PrintWriter out= resp.getWriter();
out.println(stringBuilder.toString());
out.close(); //每次执行结束都需要立即释放
}
则该段需设置stringBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");
使浏览器知道我们所采用的编码方式使中文字符串得以在浏览器显示
该段代码若记不住可在百度上按F12找到
开始第二段开始为网页添加表格和按钮样式
可通过showMsg(resp,stringBuilder.toString());
将该段代码传与第一段进行调用,可减少一样的代码的重复程度。
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("<form action='' method='post'>");
stringBuilder.append("登录名称:<input type='text' name='loginName' value=''><br>");
stringBuilder.append("登录密码:<input type='password' name='loginPwd' value=''><br>");
stringBuilder.append("<input type='reset' name='rest' value='取消'>");
stringBuilder.append("<input type='submit' name='submit' value='登录'>");
stringBuilder.append("</form>");
showMsg(resp,stringBuilder.toString());
}
第三段进行对文本的判断
若为真和假可出现不同的东西。
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String loginName= req.getParameter("loginName");
String loginPwd=req.getParameter("loginPwd");
//如果输入学号密码12345,输出你好,不然账号密码错误
String aa="请输入账号密码";
if ("3187102420".equals(loginName) && "12345".equals(loginPwd)){
aa="你好"+loginName;
}
else{
aa="请输入用户名密码";
}
showMsg(resp,aa);
}
下面为完整代码
package cn.edu.mju.project35.controllrt;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class LoginController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("<form action='' method='post'>");
stringBuilder.append("登录名称:<input type='text' name='loginName' value=''><br>");
stringBuilder.append("登录密码:<input type='password' name='loginPwd' value=''><br>");
stringBuilder.append("<input type='reset' name='rest' value='取消'>");
stringBuilder.append("<input type='submit' name='submit' value='登录'>");
stringBuilder.append("</form>");
showMsg(resp,stringBuilder.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String loginName= req.getParameter("loginName");
String loginPwd=req.getParameter("loginPwd");
//如果输入学号密码12345,输出你好,不然账号密码错误
String aa="请输入账号密码";
if ("3187102420".equals(loginName) && "12345".equals(loginPwd)){
aa="你好"+loginName;
}
else{
aa="请输入用户名密码";
}
showMsg(resp,aa);
}
private void showMsg(HttpServletResponse resp,String aa) throws IOException {
resp.setCharacterEncoding("UTF-8");
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("<html>");
stringBuilder.append("<header>");
stringBuilder.append("<title>test</title>");
stringBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");
stringBuilder.append("</header>");
stringBuilder.append("<body>");
stringBuilder.append(aa);
stringBuilder.append("</body>");
stringBuilder.append("</html>");
PrintWriter out= resp.getWriter();
out.println(stringBuilder.toString());
out.close();
}
}
总结:java代码多是通过调用方法来节省代码的重复性方便查看和节省能力。
ps:多学多做,不断前行(NL)