L
L
Loligan2016-03-19 19:40:11
Java
Loligan, 2016-03-19 19:40:11

Unable to process POST request sent from JSP page to Servelet. How to fix?

In general, I'm just trying to save the login name in a variable on the servlet. But it is impossible to even turn to Post.
mypage.jsp

<%@ page import="com.login.servlet.MainServlet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>This is page on JSP</title>
</head>
<body>
<center><h1>Hi ${log}, this is page on JSP</h1></center>
<%
  String [] strings = MainServlet.getStrings();
  for (int i = 0; i < strings.length; i++) {
%>
<%= strings[i]%> <br>
<%
  }
%>

<input type="text" size="40" name="login" value="Loligan">
<input type="submit" value="Сохранить логин в сервелете">
</body>
</html>

MainServer.java
package com.loligan.servlet;

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;
import java.util.Vector;


public class MainServlet extends HttpServlet {
    private Vector result = new Vector();
    private int n;
    String login = "no_login";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("log",login);
        req.getRequestDispatcher("mypage.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        login = req.getParameter("login");
//        resp.setContentType("text/html;charset=windows-1251");
        log("doPost entered");
        login="newLogin";

    }

    public static String [] getStrings(){
        return new String[]{"one","two","three"}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sirs, 2016-03-19
@Loligan

<form method="post" action="/">
   ...
   <input type="text" size="40" name="login" value="Loligan">
   <input type="submit" value="Сохранить логин в сервелете">
</form>

G
goshan_p, 2016-03-20
@goshan_p

Try not to use Java code in JSP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question